用户关闭窗口时识别(单击关闭按钮时)

3 macos xcode cocoa objective-c nswindow

用户关闭窗口时如何识别?

我想在窗口关闭前做点什么.

Saj*_*are 12

我在viewcontroller中使用它

//initWithNibName

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:self.view.window];
Run Code Online (Sandbox Code Playgroud)
- (void)windowWillClose:(NSNotification *)notification
    {
        NSWindow *win = [notification object];
        //...
    }
Run Code Online (Sandbox Code Playgroud)

  • 或者您可以将`NSWindowController`设置为`NSWindowsDelegate`并执行`[self.window setDelegate:self]`并在控制器类中添加` - (void)windowWillClose:(NSNotification*)notification`. (3认同)