Re: Simple NSView Question
Re: Simple NSView Question
- Subject: Re: Simple NSView Question
- From: Andy Lee <email@hidden>
- Date: Wed, 20 Aug 2008 13:14:01 -0400
On Aug 20, 2008, at 12:12 PM, Justin Giboney wrote:
I think that I am really close to this... but I can't find the next
step.
I am trying to build a view programatically after the application
has launched. So I have a custom view that has one method:
- (void)addLabelWithText:(NSString *)text
{
NSTextField *newTextField = [[NSTextField alloc]
initWithFrame:NSMakeRect(5,5,17,50)];
[newTextField setStringValue:text];
[self addSubview: newTextField];
//[self setNeedsDisplay:YES];
}
If I add the code above into the drawRect: method, it works just fine.
Technical questions would be so much clearer if we could remove
"works" and "doesn't work" from the English language. :) By "works"
do you mean it compiles? It runs without crashing? I would expect it
not to work as you probably want. -drawRect: is for drawing, and
drawing only. It shouldn't modify the view hierarchy and it shouldn't
invalidate the display, since both of these actions will invalidate
part of the view's display area, causing -drawRect: to be called
again, whicn will again invalidate part of the view, and so on. And
you probably don't mean to add a subview every time the view draws.
I think I need the commented line, but whether it is commented or
not, it doesn't work. What do I have to do to redraw the view with
the text field?
I assume your custom view comes from a nib file? Try adding an -
awakeFromNib method and adding any subviews you want there. This is
assuming, of course, that you have a good reason for adding the
subviews in code rather than in the nib file in the first place --
perhaps because your view layout is dynamic in some way and can't be
decided until launch time?
--Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden