我目前正在使用以下代码显示模式窗口:
[[NSApplication sharedApplication] runModalForWindow:mainWindow];
但是,当我关闭此窗口时,其他窗口仍处于非活动状态。stopModal
使用“red x”关闭窗口时如何运行该方法?
谢谢,
迈克尔
我目前正在使用以下代码显示模式窗口:
[[NSApplication sharedApplication] runModalForWindow:mainWindow];
但是,当我关闭此窗口时,其他窗口仍处于非活动状态。stopModal
使用“red x”关闭窗口时如何运行该方法?
谢谢,
迈克尔
您可以为窗口创建一个委托并让它响应
-(void)windowWillClose:(NSNotification *) 通知或
- (void)windowShouldClose:(NSNotification *) 通知方法,如下所示:
- (void)windowWillClose:(NSNotification *)notification {
[[NSApplication sharedApplication] stopModal];
}
如果您有一个适用于特定窗口的对话框,那么您可能不应该使用模式对话框,而是使用工作表。如果可能,应避免使用模态对话框。如果您使用工作表,那么您遇到的问题将不再是问题。
- (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];
}
除了Randall 的回答,您还可以将控制器类链接为 .xib 文件中定义的窗口的委托。
你可以处理
[[NSApplication sharedApplication] stopModal];
在任一
-(void)performClose:(id)sender
-(void)windowWillClose:(NSNotification *)notification
方法。
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)
}