Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 792
zoul Avatar asked Jun 28 '12 17:06

zoul


1 Answers

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]; 
}
like image 98
Anne Avatar answered Oct 04 '22 23:10

Anne