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"?

Any better workaround for this?  (-superview doesn't help).

Jerry Krinock

[1] Example: In a subclass of NSOutlineView....

- (void)awakeFromNib ;
    ...
    MyDocument* doc = [[[self window] windowController] document] ;

    // Register some observers
    [[NSNotificationCenter defaultCenter] addObserver:self
                              selector:@selector(deselectAll:)
name:NSUndoManagerWillUndoChangeNotification
                                object:[doc undoManager]] ;
    [[NSNotificationCenter defaultCenter] addObserver:self
                              selector:@selector(deselectAll:)
name:NSUndoManagerWillRedoChangeNotification
                                object:[doc undoManager]] ;
    // Initialize the data source and tell it the
    // document from which it can get its content.
    BmDataSource* ovds = [[BmDataSource alloc] initWithDoc:doc] ;
    ...

[2] http://sheepsystems.com/engineering/TabTest.zip

[3] Workaround, in any NSView subclass.

IBOutlet NSWindow* windowWorkaround ;

- (NSWindow*)window {
    NSWindow* window_ = windowWorkaround ;
    if (!window_){
        NSLog(@"Whoops! Connect windowWorkaround outlet in IB for %@",
             [self class]) ;

        // This will work only if our super-NSTabView is selected...
        window_ = [super window] ;
    }
    return window_ ;
}

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to