How to detect that my window is being closed using the red window button?

▼魔方 西西 提交于 2019-12-06 02:32:40

问题


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?

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.


回答1:


Define close button:

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


来源:https://stackoverflow.com/questions/11249822/how-to-detect-that-my-window-is-being-closed-using-the-red-window-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!