NSWindowController: loadWindow loads window from nib but showWindow: does nothing

我的未来我决定 提交于 2019-12-24 01:19:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!