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

I have a dialog box that can be canceled using the user’s Cancel button or the red system button of the system. I need to follow some simple logic when the dialog is canceled. How to determine that the user has pressed the red button?

I know that I find that the window closes with the delegate -windowWillClose: But this callback is also called when I close the window programmatically after the dialog is successfully completed. I also know that I can just set the BOOL flag, but is there a better solution? It would be better if I could detect the activation of the red button.

+4
source share
1 answer

Define the close button:

 NSButton *closeButton = [self standardWindowButton:NSWindowCloseButton]; 

Connect the close button to the custom selector:

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

Run a special code and close the window manually.

 -(void)closeThisWindow { // // The NSWindowCloseButton has been clicked. // Code to be run before the window closes. // [self close]; } 
+13
source

All Articles