问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/3539721/nswindowcontroller-loadwindow-loads-window-from-nib-but-showwindow-does-nothin