views:

41

answers:

1

I have an NSWindowController subclass called _PreferencesWindowController with the following implementation -

@synthesize window;

- (id)init {

 self = [super initWithWindowNibName:@"PreferencesWindow"];
 if (!self) return nil;

 return self;
}

And I tried to show the window in _PreferencesWindowController by using the following code -

_preferencesWindowController = [[_PreferencesWindowController alloc] init];
[_preferencesWindowController showWindow:nil];

It does nothing, and I checked _preferencesWindowController.window is nil from the debugger.

However if I call loadView on _preferencesWindowController the window can be loaded and is visible; _preferencesWindowController.window is no longer nil-valued -

[_preferencesWindowController loadWindow];

I looked at Apple's documentation on NSWindowController it specifically says "you should never directly invoke loadWindow", instead showWindow: should be used. I'm wondering what I might have missed that resulted in the above-mentioned behaviour I have been seeing.

A: 

OK I solved this by looking at the NSWindowController header file.

The problem is in my header file for _PreferencesWindowController -

@interface _PreferencesWindowController : NSWindowController <NSToolbarDelegate> {

    NSWindow *window;

}

@property (assign) IBOutlet NSWindow *window;

@end

By removing the @property declaration and changing NSWindow *window ivar to IBOutlet NSWindow *window, showWindow: method now works without a glitch.

The property declaration must have resulted in an undefined behaviour in showWindow: method in NSWindowController's implementation.

Adam Ko
Declaring the property was not the problem, but synthesizing it might have been—NSWindowController already has an implementation for `window`, and it's more than a simple property accessor. Having the ivar is also wrong, regardless of whether you declare it with `IBOutlet` (which doesn't matter to the compiler or at run time), because NSWindowController already has an outlet ivar by that name. You should cut out both the ivar *and* the property.
Peter Hosey
You are right. My subclass sees the `window` ivar in Interface Builder regardless whether I've declared the ivar. And your suggestion solved another problem I had with `-[NSOpenPanel beginSheetModalForWindow: completionHandler:]` Thanks! :)
Adam Ko