我想在用户单击 NSWindowController 中的红色关闭按钮时停止模式。
在 NSWindowController 中,有“确定”和“取消”按钮。
- (IBAction)okButtonClicked:(id)sender
{
[NSApp stopModalWithCode:NSOKButton];
[self.window close];
}
- (IBAction)cancelButtonClicked:(id)sender
{
[NSApp stopModalWithCode:NSCancelButton];
[self.window close];
}
当我单击红色关闭按钮时,窗口将关闭并且模式不会停止。 我找到了 windowWillClose: 函数。
- (void)windowWillClose:(NSNotification *)notification
{
if ([NSApp modalWindow] == self.window)
[NSApp stopModal];
}
但是,
if ([NSApp runModalForWindow:myWindowController.window] != NSOKButton)
return;
即使我点击 OK 按钮,windowWillClose: 函数也会被调用并且 runModalForWindow: 函数总是返回 NSCancelButton。
我可以将成员变量添加到 myWindowController 作为模态的结果。
但我认为会有另一种通用方法来解决这个问题。
我想走个简单的路。
最佳答案
也许有点晚了,但我只是发现了这个问题,试图为自己找到答案。这就是官方文档中所说的:有 - (BOOL)windowShouldClose:(id)sender
事件处理程序,当您通过 [window close] 关闭窗口时不会调用它。它仅在您使用红色关闭按钮或 [window performClose:] 选择器时调用。所以解决方案是在 NSWindowController 子类中实现 windowShouldClose:
而不是 windowWillClose:
。
关于objective-c - 如何使用 NSWindowController 中的关闭按钮停止模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695620/