Re: -[NSView window] returns nil if its NSTabView not selected
Re: -[NSView window] returns nil if its NSTabView not selected
- Subject: Re: -[NSView window] returns nil if its NSTabView not selected
- From: Quincey Morris <email@hidden>
- Date: Wed, 7 Jan 2009 20:41:24 -0800
On Jan 7, 2009, at 19:36, Jerry Krinock wrote:
In a view's -awakeFromNib, I often do some initializations which
require the view's -window [1].
This worked fine until I put one of these views inside an
NSTabView. Now, it fails if the tab containing the view is not
selected when the nib is loaded, because in this case -[NSView
window] returns nil. Since I don't see this fact in the
documentation, I made myself a tiny test project [2], and indeed,
that's what happens!
I worked around my immediate problem by defining an outlet and
wiring it directly to the window [3], but I can see myself
forgetting to do this and having to plow through some mysterious bug
every few months from now until eternity.
"When an object receives an awakeFromNib message, it is guaranteed
to have all its outlet instance variables set." Is not -window kind
of an "outlet instance variable"?
Well, it *is* set to the correct value -- the correct value just
happens to be nil if the tab is not selected.
You can't really claim that an unselected tab being removed from the
view hierarchy (which is why it has no window) is a bug -- it's just
how NSTabView happens to be implemented.
The real issue, I'd suggest, is in your statement:
In a view's -awakeFromNib, I often do some initializations which
require the view's -window [1].
awakeFromNib isn't (in general) the correct place to do
initializations that require the window. For example, if your tab
view's enclosing view was in a NIB loaded by a view controller instead
of a window controller (such things are not uncommon, now that
NSViewController makes NIB factoring easy) then there just may be no
window at all at awakeFromNib time. Initializations that require the
window should be done in viewDidMoveToWindow:.
The only part that needs some thought is initializations which require
both a window and NIB loading to be complete. For that, I've found the
following unexciting pattern to be useful:
- (void) awakeInWindow {
// initializations here can assume a window, and nib loading complete
}
- (void) awakeFromNib {
isAwakeFromNib = YES;
if (self.window)
[self awakeInWindow];
}
- (void) viewDidMoveToWindow {
if (self.window && isAwakeFromNib)
[self awakeInWindow];
}
where isAwakeFromNib is an instance variable.
_______________________________________________
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