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

时间:2023-01-20 17:04:04

How can i recognize when user close a window ?

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

i want to do something before window close.

我想在关门前做点什么。

2 个解决方案

#1


12  

I use it in a viewcontroller

我在viewcontroller中使用它

//initWithNibName

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

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

#2


1  

You can declare your custom class to conform to NSWindowDelegate protocol.

您可以声明您的自定义类符合NSWindowDelegate协议。

Set an instance of your custom class to be the delegate of your window

将自定义类的实例设置为窗口的委托

Then use one of these methods (probably the windowWillClose: one) to do something before the window closes.

然后使用这些方法中的一个(可能是windowWillClose: one)在窗口关闭之前执行一些操作。

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

#1


12  

I use it in a viewcontroller

我在viewcontroller中使用它

//initWithNibName

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

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

#2


1  

You can declare your custom class to conform to NSWindowDelegate protocol.

您可以声明您的自定义类符合NSWindowDelegate协议。

Set an instance of your custom class to be the delegate of your window

将自定义类的实例设置为窗口的委托

Then use one of these methods (probably the windowWillClose: one) to do something before the window closes.

然后使用这些方法中的一个(可能是windowWillClose: one)在窗口关闭之前执行一些操作。

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