如何检测我的窗口正在使用红色窗口按钮关闭? [英] How to detect that my window is being closed using the red window button?

查看:228
本文介绍了如何检测我的窗口正在使用红色窗口按钮关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a dialog window that can be cancelled through a custom Cancel button or using the system red window button. I need to perform some simple logic when the dialog is cancelled. How do I detect that the user has pressed the red button?

我知道我可以使用 -windowWillClose: code> delegate回调。但是当我在对话成功后以编程方式关闭窗口时,也会调用此回调。我也知道我可以简单地设置一个 BOOL 标志,但有更好的解决方案吗?

I know I can detect the window being closed using the -windowWillClose: delegate callback. But this callback is also called when I close the window programmatically after the dialog succeeds. I also know I could simply set up a BOOL flag, but is there a better solution? It would be best if I could detect the red button activation.

推荐答案

定义关闭按钮:

NSButton *closeButton = [self standardWindowButton:NSWindowCloseButton];

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

Connect close button to custom selector:

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

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

Run specific code and close window manually.

-(void)closeThisWindow {

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

    [self close]; 
}

这篇关于如何检测我的窗口正在使用红色窗口按钮关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆