Re: Simple NSView Question
Re: Simple NSView Question
- Subject: Re: Simple NSView Question
- From: Justin Giboney <email@hidden>
- Date: Wed, 20 Aug 2008 11:28:15 -0600
Here are my comments:
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?
By "works" I mean that it displays the text field in the view.
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.
I have an -awakeFromNib method in my AppController class which calls
the -addLabelWithText: method. I can even put this code in that -
awakeFromNib method, and I don't see anything in the view. (the view
is an IBOutlet of the AppController class)
NSTextField *newTextField = [[NSTextField alloc]
initWithFrame:NSMakeRect(5,5,100,25)];
[newTextField setStringValue:@"hello"];
[mainView addSubview: newTextField];
[mainView setNeedsDisplay:YES];
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?
Yes I have a good reason. I don't have to have a dynamic layout, but I
plan on having over 100 different views, which would be difficult to
create, and difficult to update or add more.
I would think that by adding the text view to the hierarchy that it
would remain there every time the view gets redrawn, but apparently it
doesn't. How do I keep it there?
Thanks,
Justin Giboney
_______________________________________________
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