One of my classes allocates a custom NSWindowController, how can my class know when the window gets closed?

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

What I'm trying to do is have the original class (the one that allocates the custom window controller) know when the window is closed so that I can set wc = nil when the window is no longer needed :)

有帮助吗?

解决方案

If your NSWindowController class is set as the window's delegate, you can simply respond to the -windowWillClose: method.

- (void)windowWillClose:(NSNotification *)notification
{
    /* ... */
}

Otherwise, since this is also a notification, you can register to receive the notification from any class.

- (void)myWindowWillClose:(NSNotification *)notification
{
    /* ... */
}

...
CustomNSWindowController *wc = ...;
[[NSNotificationCenter defaultCenter]
    addObserver:self
    selector:@selector(myWindowWillClose:)
    name:NSWindowWillCloseNotification
    object:[wc window]];
[wc showWindow:self];

See NSWindow Class Reference, NSWindowDelegate Protocol Reference

其他提示

I would guess that you could either send a notification, or make your parent class the delegate of the CustomNSWindowController.

[edit] - Dietrich is right - I forgot about the NSWindow delegate protocol. You could set the parent class as the delegate of the window of the windowController

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top