What is responsible for releasing NSWindowController objects?

后端 未结 2 1063
情深已故
情深已故 2021-01-14 20:06

Lets say I display a window like so:

[[TBAddTaskWindowController new] showWindow:self];

Where is the TBAddTaskWindowController o

相关标签:
2条回答
  • 2021-01-14 20:52

    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.

    0 讨论(0)
  • 2021-01-14 20:56

    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.


    [added information for working under ARC]

    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 opened
    • pnl_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.

    0 讨论(0)
提交回复
热议问题