Objective-c – Redrawing an NSView

cocoansviewobjective c

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!

Best Answer

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];
Related Topic