Re: Release a NSWindowController after the window is closed
Re: Release a NSWindowController after the window is closed
- Subject: Re: Release a NSWindowController after the window is closed
- From: Kyle Sluder <email@hidden>
- Date: Sat, 18 Jun 2011 10:09:13 -0700
On Wed, Jun 15, 2011 at 11:40 AM, Brian Norh <email@hidden> wrote:
> I'm building a Cocoa application and have a question about using
> window controllers. The idea is that when the user selects New from
> the File menu, an instance of MyWindowController which is a subclass
> of NSWindowController is created and a new window from MyWindow.xib is
> displayed.
>
> I'm handling the action in the application delegate. From what I have
> seen after searching around something like the following could be
> done. Once the window is displayed I don't have any reason to store a
> pointer to the window controller anymore and since I allocated it I
> also have it autoreleased before displaying the window.
This general pattern will fail under ARC (yay, we can talk about that
now). Since you're not storing the object into a strong reference, it
will be immediately deallocated. And it's reasonable to think that in
the future you might want to message the window controller from your
app delegate. So why not keep an instance variable in your app
delegate that points to the window controller, or perhaps to an array
of such window controllers. In your window controller's
-windowWillClose implementation, your window controller can call back
to the app delegate, telling it that it's going away.
It's essentially the same delegate/owner pattern you see all over the
place in Cocoa, except the window controller doesn't need a weak
pointer to the app delegate because it cal always get at it by calling
[NSApp delegate].
>
> [[[[MyWindowController alloc] init] autorelease] showWindow:self];
>
> Since the window is released soon afterwards the window will briefly
> display on the screen and then go away. I'm using a solution where I
> retain the window controller in the -showWindow: method and let it
> release itself once it gets a windowWillClose notification.
>
> - (IBAction)showWindow:(id)sender
> {
> void (^windowWillCloseHandler)(NSNotification *) = ^(NSNotification *note) {
> [[NSNotificationCenter defaultCenter] removeObserver:self
> name:NSWindowWillCloseNotification object:self.window];
> [self release];
> };
>
> [self retain];
> [[NSNotificationCenter defaultCenter]
> addObserverForName:NSWindowWillCloseNotification object:self.window
> queue:nil usingBlock:windowWillCloseHandler];
> [super showWindow:sender];
> }
NSWindow automatically signs its delegate up for
NSWindowWillCloseNotification. In 99.99% of cases, you're going to
want your window controller to also be your window's delegate. So you
shouldn't have to do this yourself.
Even if you were to do this yourself, -windowDidLoad would be a more
appropriate place than -showWindow: to set up this observer.
--Kyle Sluder
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden