如何使用红色窗口按钮检测我的窗口是否正在关闭?

时间:2012-06-28 17:23:53

标签: cocoa nswindow nswindowcontroller

我有一个对话窗口,可以通过自定义取消按钮或使用系统红色窗口按钮取消。取消对话框时,我需要执行一些简单的逻辑。如何检测用户是否按下了红色按钮?

我知道我可以使用-windowWillClose:委托回调来检测正在关闭的窗口。但是,当对话成功后以编程方式关闭窗口时,也会调用此回调。我也知道我可以简单地设置一个BOOL标志,但是有更好的解决方案吗?如果我能检测到红色按钮激活,那将是最好的。

1 个答案:

答案 0 :(得分:14)

定义关闭按钮:

NSButton *closeButton = [self standardWindowButton:NSWindowCloseButton];

将关闭按钮连接到自定义选择器:

[closeButton setTarget:self.delegate];
[closeButton setAction:@selector(closeThisWindow)]; 

手动运行特定代码并关闭窗口。

-(void)closeThisWindow {

    //
    // The NSWindowCloseButton has been clicked.
    // Code to be run before the window closes.
    //

    [self close]; 
}
相关问题