2

我的一个班级分配了一个 custom NSWindowController,我的班级如何知道窗口何时关闭?

CustomNSWindowController *wc = [[CustomNSWindowController alloc] init];
[wc showWindow:self];
//how to detect when window is closed?

我想要做的是让原始类(分配自定义窗口控制器的那个)知道窗口何时关闭,以便我可以在不再需要窗口时设置 wc = nil :)

4

3 回答 3

5

如果您的NSWindowController类被设置为窗口的委托,您可以简单地响应该-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 类参考NSWindowDelegate 协议参考

于 2012-10-31T17:56:27.583 回答
0

我猜你可以发送一个通知,或者让你的父类成为 CustomNSWindowController 的代表。

[编辑] - 迪特里希是对的 - 我忘记了 NSWindow 委托协议。您可以将父类设置为 windowController 的窗口的委托

于 2012-10-31T17:57:38.620 回答