Re: retaining and releasing a Window Controller.
Re: retaining and releasing a Window Controller.
- Subject: Re: retaining and releasing a Window Controller.
- From: Quincey Morris <email@hidden>
- Date: Mon, 9 Aug 2010 00:21:07 -0700
On Aug 8, 2010, at 23:27, Kevin Bracey wrote:
> I have a Report WindowController, that opens a Window from it's own nib. This is great, I can open lots of different Reports all bases on the same Controller. But I only need the Window and the controller to stay around while the Window is open. It's all self contained and nothing else needs reference it.
>
> Is it ok to [self retain] in the Controller's init, then [self release] in the - (void)windowWillClose:(NSNotification *)notification. Is that the best place to do this, I'm not at all sure about an object retaining and releasing itself.
>
> at the moment I'm storing all these reports in an array to stop the controller disappearing out from underneath me and then removing them when the Window closes, I'm sure there's a better way.
Either approach is viable.
There's no inherent reason why an object shouldn't retain and release itself. In general, doing so might indicate a design problem (such as an abuse of the MVC paradigm), but in specific cases like this you can make an argument that it's a natural approach.
OTOH, I'm not sure why you think the array of controllers approach is "convoluted". It's also a perfectly natural approach, and it's what NSDocument objects do to manage their own window controllers.
In fact, the two approaches are more or less identical. In the second one, [windowControllers addObject: newWindowController] has the same purpose as [newWindowController retain] in the first one -- to preserve a strong reference to the window controller -- and removeObject/release are similarly equivalent in purpose.
My choice would actually be the array of controllers approach, because it sometime turns out -- further down the line, when your application's UI gets refined further -- that you want to know what the list of controllers is, or to maintain an ordering for the window controllers. For example, sometimes you want to manipulate the appearance of the Window menu's list of open windows, and you need to control what that list should look like. Even if you never need to consult the list, you're only wasting (basically) one line of code -- to create the array initially -- although in practice I'd make the list be a property of the app delegate in order to regularize the access.
_______________________________________________
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