xcode 识别用户何时关闭窗口(点击关闭按钮时)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10060798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recognize when user close a window (when click on close button)
提问by SajjadZare
How can i recognize when user close a window ?
我如何识别用户何时关闭窗口?
i want to do something before window close.
我想在窗口关闭之前做点什么。
回答by SajjadZare
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];
//...
}
回答by Jesse Black
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