문제

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