objective-cxcodemacoscocoanswindow

Recognize when user close a window (when click on close button)


How can i recognize when user close a window ?

i want to do something before window close.


Solution

  • I use it in a viewcontroller

    //initWithNibName
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:self.view.window];
    

    - (void)windowWillClose:(NSNotification *)notification
        {
            NSWindow *win = [notification object];
            //...
        }