Lets say I display a window like so:
[[TBAddTaskWindowController new] showWindow:self];
Where is the TBAddTaskWindowController o
The same code that instantiated the window controller by sending the new
message to the class, just the same as if it had done it by alloc
and init
messages.
Yes, a common way to do release the window controller is with:
- (void)windowWillClose:(NSNotification *)notification
{
[self autorelease];
}
The Window Controller needs to live only as long as the window is around, so autoreleasing it when the window goes away makes perfect sense.
Remember to remove any other observers, etc as well.
For ARC, you need to retain a strong reference to the window control while the window is open, and then remove it when the window closes.
To do this, I added a category on the window controlled with two methods:
pnl_addWindowController
— called by the window controller when the window is first openedpnl_removeWindowController
— called from windowWillClose
The category maintains a global NSMutableSet
of active window controllers. The code is essentially just [gWindowControllers addObject:self]
and [gWindowControllers removeAllObjects]
, with some lazy creation of the NSMutableSet and some locking.