Recognize when the user closes the window (when the close button is clicked)

How can I know when the user closes the window?

I want to do something before the window is closed.

+3


source to share


2 answers


I am using it in viewcontroller

//initWithNibName

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

      




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

      

+12


source


You can declare your own class to be protocol compliant NSWindowDelegate

.

Set an instance of your custom class as your window delegate



Then use one of these methods (perhaps windowWillClose: one) to do something before the window is closed.

- (BOOL)windowShouldClose:(id)sender
- (void)windowWillClose:(NSNotification *)notification

      

+1


source







All Articles