objective-c - NSClosableWindowMask 对话框行为异常

标签 objective-c macos nswindow

我有一个 Safari 浏览器插件,我想在其中打开一个 NSWindow 来显示版权说明。虽然对话框中的“确定”按钮关闭了对话框的窗口,并且运行良好,但当我单击左上角的红色关闭窗口“x”时,它也关闭了窗口,但它是父窗口(整个浏览器选项卡,我插件在运行),仍然处于禁用状态,就好像模态窗口在某处打开一样。

我什至尝试将一个新的选择器附加到窗口关闭通知,它运行与确定按钮相同的代码,但仍然无法正常工作。

这是代码的相关部分:

- (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];   
}

最佳答案

我已经修改了您的代码,尝试如下所示:-

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

    [mwin performClose:mwin];// Modified this line 


}

   //Modified below notification just comment the parameter
    - (void)closeClicked/*:(NSNotification *)notification*/
    {

    [NSApp abortModal];

}


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

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(closeClicked)
                                                 name:NSWindowWillCloseNotification
                                               object:nil];
    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];   
}

关于objective-c - NSClosableWindowMask 对话框行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491913/

相关文章:

cocoa - 维护 NSWindow 上 View 的宽高比,而不是宽高比。

macos - 调整大小时 NSWindow 重画问题/闪烁

iphone - JSONKit + 利登布罗克 VS. RestKit?(使用 CoreData)

iphone - MKMap 不使用定义的纬度和经度

ios - 如何在 Swift 中为 SpriteKit 定义类别位掩码枚举?

objective-c - 如何从文件中截断 x 字节

ios - 从 OS X 向 iOS 发送通知

macos - 从启动守护程序生成 screen session

ios - cocoa 最大化调整内容大小的事件

c++ - 未找到符号,应在平面命名空间 ObjC++ 中