NSClosableWindowMask对话框表现奇怪

Mko*_*och 6 macos objective-c nswindow

我有一个Safari浏览器插件,在其中,我想打开一个NSWindow来显示版权说明.当对话框中的Ok按钮关闭对话框的窗口,并且工作正常时,当我点击左上角的红色关闭窗口"x"时,它也会关闭窗口,但它是父窗口(整个浏览器选项卡,我让插件运行),仍然被禁用,好像仍然在某处打开模态窗口.

我甚至试图将一个新的选择器附加到窗口关闭通知,该选项运行与Ok按钮相同的代码,但是,它仍然无法正常工作.

以下是代码的相关部分:

- (void) closeBox
{
    // called when the Ok button pressed
    [NSApp abortModal];

}

- (void)closeClicked:(NSNotification *)notification
{
    // called when the close window 'x' button pressed
    NSLog(@"Closed");
    [NSApp abortModal];
}


- (void) openBox
{
    NSRect frame = NSMakeRect(0, 0, 300, 250);
     mwin  = [[[NSWindow alloc] initWithContentRect:frame
                                     styleMask:NSClosableWindowMask |NSTitledWindowMask
                                       backing:NSBackingStoreBuffered
                                         defer:NO] autorelease];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(closeClicked:)
                                             name:NSWindowWillCloseNotification
                                           object:mwin];
    NSButton * bn;
    bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];

    [bn setButtonType:NSMomentaryPushInButton];
    [bn setTitle:@"Ok"];
    [bn setTarget:self];
    [bn setAction:@selector(closeBox)];

    [[mwin contentView] addSubview:bn];

    [NSApp runModalForWindow:mwin];   
}
Run Code Online (Sandbox Code Playgroud)

Mko*_*och 0

经过几个小时的谷歌搜索,我发现其他人也有类似的问题,解决方案非常简单:窗口对象被 NSNotificationCenter 的观察者保留。我所要做的就是删除观察者:

- (void) closeClicked:(NSNotification *)notification
{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [NSApp abortModal];

    NSLog(@"Closed");

}
Run Code Online (Sandbox Code Playgroud)

现在它可以正常工作了。