Pregunta

I want to be able to create multiple instances of a window in a Cocoa application. Therefore I have created a NSWindowController and associated XIB. In my application delegate I have the following code:

- (IBAction)newWindow:(id)sender
{
    MyWindowController *wc = [[MyWindowController alloc]
            initWithWindowNibName:@"MyWindow"];
    [self.windowControllers addObject:wc];
    [wc showWindow:self];
}

I have to keep a pointer to the window controller somewhere, since ARC will otherwise dealloc it. I've found that a common solution is to add it to a mutable array. However, if the window is later closed the reference will still be in the array and therefore not deallocated.

What's the best way to remove the window controller from the array when the window is closed?

¿Fue útil?

Solución

You can use - (BOOL)windowShouldClose:(id)sender instance method of NSWindowDelegate.

I could not find it in the documentation, but try using (BOOL)windowWillClose:(id)sender first.

See here.

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