Pregunta

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 :)

¿Fue útil?

Solución

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

Otros consejos

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

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top