首页 文章

NSWindowController无法第二次打开NSWindow

提问于
浏览
0

我有一个NSWindowController子类,BBPreferencesWindowController

@implementation BBPreferencesWindowController

- (NSString *)windowNibName
{
    return @"PreferencesWindow";
}

...

我的AppDelegate中的一个函数可以通过此控制器打开“PreferencesWindow.xib”中的窗口 .

此函数从NSMenuItem调用,附加到系统菜单栏中NSStatusItem项下的NSMenu .

@property (strong) BBPreferencesWindowController *preferencesWindow;

...

- (void)openPreferences
{
    if (self.preferencesWindow == nil)
    {
        self.preferencesWindow = [[BBPreferencesWindowController alloc] init];
    }

    [self.preferencesWindow showWindow:self];
    NSLog(@"%@", self.preferencesWindow.window.isVisible ? @"YES" : @"NO");
}

第一次单击NSMenuItem时窗口显示正常(虽然NSLog行记录“NO”),但是当我关闭窗口然后再次通过单击NSMenuItem重新打开它时,窗口赢了打开 .

我错过了什么?

谢谢!

Edit: BBPreferencesWindowController 没有自定义init方法 . 它有自定义 awakeFromNib (第一次调用)

- (void)awakeFromNib
{
    [super awakeFromNib];
    NSLog(@"Loaded!");
}

1 回答

  • 3

    我找到了 BBPreferencesWindowController 没有很好地管理窗口的原因:在XIB中,文件所有者 window 出口没有正确链接 .

    修复此问题也解决了所有其他问题 .

    谢谢你的帮助!

相关问题