简体   繁体   中英

Redrawing an NSView

Sorry if this has been asked before or it's a really dumb question, but I can't figure it out. I have an NSView in my interface, and I have created a subclass of NSView in Xcode. Then using the identity inspector, I set my NSView's class to be the newly created NSView subclass. The view draws fine, but now I need to redraw it to change a string inside the view. I'm pretty sure this has to do with setNeedsDisplay, but what do I send the message to? I don't have a particular instance of my view in the code, since it's in Interface Builder, so what do I do? Again, sorry if this is dumb. I haven't done much with NSView yet. Ask for more info if you need it. Thanks!

In the view controller subclass you have, add an ivar with type of your NSView subclass. Declare a property on it, and mark it as an outlet.

// ViewControllerSubclass.h
ViewType *myView;

@property(readwrite, assign) IBOutlet ViewType *myView;

// ViewControllerSubclass.m
@synthesize myView;

Now you have an outlet, connect it to the view you designed via IB. To do so, right click in IB on your view controller subclass (the file's owner), you should see the outlet in the list.

Once you have done that, you are now able to send messages to the view in your code.
To mark the view as needing redraw :

[myView setNeedsDisplay:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM