我的一个类分配了一个自定义的NSWindowController
,我的类如何知道窗口何时关闭?
CustomNSWindowController *wc = [[CustomNSWindowController alloc] init];
[wc showWindow:self];
//how to detect when window is closed?
我想做的是让原始类(分配自定义窗口 Controller 的类)知道窗口何时关闭,以便我可以在不再需要窗口时设置 wc = nil :)
最佳答案
如果您的 NSWindowController
类设置为窗口的委托(delegate),您只需响应 -windowWillClose:
方法即可。
- (void)windowWillClose:(NSNotification *)notification
{
/* ... */
}
否则,由于这也是一个通知,您可以注册以接收来自任何类的通知。
- (void)myWindowWillClose:(NSNotification *)notification
{
/* ... */
}
...
CustomNSWindowController *wc = ...;
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(myWindowWillClose:)
name:NSWindowWillCloseNotification
object:[wc window]];
[wc showWindow:self];
参见NSWindow Class Reference , NSWindowDelegate Protocol Reference
关于objective-c - 检测窗口何时关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13163904/