5

我目前正在使用以下代码显示模式窗口:

[[NSApplication sharedApplication] runModalForWindow:mainWindow];

但是,当我关闭此窗口时,其他窗口仍处于非活动状态。stopModal使用“red x”关闭窗口时如何运行该方法?

谢谢,

迈克尔

4

4 回答 4

10

您可以为窗口创建一个委托并让它响应
-(void)windowWillClose:(NSNotification *) 通知
- (void)windowShouldClose:(NSNotification *) 通知方法,如下所示:

- (void)windowWillClose:(NSNotification *)notification {
   [[NSApplication sharedApplication] stopModal];
}

请参阅Mac 开发中心:NSWindowDelegate 协议参考

于 2010-01-06T00:16:33.120 回答
2

如果您有一个适用于特定窗口的对话框,那么您可能不应该使用模式对话框,而是使用工作表。如果可能,应避免使用模态对话框。如果您使用工作表,那么您遇到的问题将不再是问题。

- (void)showSheet:(id)sender
{
    [NSApp beginSheet:yourModalWindow 
        modalForWindow:windowThatSheetIsAttachedTo
        modalDelegate:self
        didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) 
           contextInfo:nil];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
    [sheet orderOut:self];
    [NSApp endSheet:sheet];
}
于 2010-01-06T03:00:36.083 回答
0

除了Randall 的回答,您还可以将控制器类链接为 .xib 文件中定义的窗口的委托。

你可以处理

[[NSApplication sharedApplication] stopModal];

在任一

  • -(void)performClose:(id)sender

  • -(void)windowWillClose:(NSNotification *)notification

方法。

于 2015-06-12T22:02:05.187 回答
0

Swift 4(请注意,以前的方法已弃用):

window.beginSheet(self.uiSettingsPanel, completionHandler: {response in
        NSLog("Finished sheet, response: \(response)")
})

其中 self.uiSettingsPanel 是 NSPanel 子类的实例。然后,在工作表的 NSPanel 子类中,用类似的东西关闭它

@IBAction func buttonOK(_ sender: NSButton) {
    self.sheetParent!.endSheet(self, returnCode: .OK)
}
于 2019-01-23T22:59:10.777 回答