【问题标题】:How to detect that my window is being closed using the red window button?如何使用红色窗口按钮检测我的窗口正在关闭?
【发布时间】:2012-06-28 17:23:52
【问题描述】:

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

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

【问题讨论】:

  • 同时拥有取消按钮和关闭按钮不是正确的 UI(至少在 Mac 上不是)。在对话框中,存在按钮,因此它们就足够了。在这种情况下,窗口框架中应该没有启用的红色按钮;在 Cocoa 中,这是通过设置窗口的掩码来实现的。

标签: cocoa nswindow nswindowcontroller


【解决方案1】:

定义关闭按钮:

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]; 
}

【讨论】:

  • 谢谢,这正是我想要的。
  • 我使用了同样的技巧,而且效果很好,但要小心 windowShouldClose: 不会被调用
猜你喜欢
  • 2012-02-16
  • 1970-01-01
  • 2012-05-14
  • 2014-08-23
  • 1970-01-01
  • 2018-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
相关资源
最近更新 更多