【发布时间】:2012-04-08 06:15:16
【问题描述】:
如何识别用户何时关闭窗口?
我想在关闭窗口之前做一些事情。
【问题讨论】:
标签: objective-c xcode macos cocoa nswindow
如何识别用户何时关闭窗口?
我想在关闭窗口之前做一些事情。
【问题讨论】:
标签: objective-c xcode macos cocoa nswindow
我在视图控制器中使用它
//initWithNibName
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:self.view.window];
- (void)windowWillClose:(NSNotification *)notification
{
NSWindow *win = [notification object];
//...
}
【讨论】:
NSWindowController 设置为NSWindowsDelegate 并执行[self.window setDelegate:self] 并在您的控制器类中添加- (void)windowWillClose:(NSNotification *)notification。
您可以声明您的自定义类以符合NSWindowDelegate 协议。
将自定义类的实例设置为窗口的委托
然后使用这些方法之一(可能是 windowWillClose: one)在窗口关闭之前做一些事情。
- (BOOL)windowShouldClose:(id)sender
- (void)windowWillClose:(NSNotification *)notification
【讨论】: