Release a NSWindowController after the window is closed
Release a NSWindowController after the window is closed
- Subject: Release a NSWindowController after the window is closed
- From: Brian Norh <email@hidden>
- Date: Wed, 15 Jun 2011 20:40:25 +0200
Hello.
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.
[[[[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];
}
Is there a better way to do this? I have searched the documentation
and have not found anything specific on which practices to use. It
sounds like something very basic which it should cover so maybe I'm
just searching with the wrong terms.
Brian
_______________________________________________
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