On Sat, Aug 2, 2008 at 11:23 AM, Torsten Curdt <[EMAIL PROTECTED]> wrote: > Hey guys, > > Let's say in my AppController a new Non-modal window is getting opened like > this > > - (IBAction) buttonOpen:(id)sender > { > MyWindowController *controller = [[MyWindowController alloc] init]; > [controller showWindow:self]; > } > > Of course I will have to release the controller at some stage. Now there are > a couple of ways doing this: > > 1. Inside the window controller call [self close]; [self release]; > > Not really sure this would actually be OK as the release could happen to > soon. > > 2. Inside the window controller [self close]; [self autorelease]; > > This would delay the release of the controller. > > But with both methods the AppController class does not have any idea the > window got closed (and released). Which can be a problem if you want to act > on this event. So there is another option: > > 3. Create a showWindow: andCallOnClose: and pass in a selector and then call > the selctor on close > > - (IBAction) buttonOpen:(id)sender > { > MyWindowController *controller = [[MyWindowController alloc] init]; > [controller showWindow:self andCallOnClose:@selector(windowClose:)]; > } > > - (void) windowClosed:(id)controller > { > [controller release]; > } > > But I wouldn't be surprised if there was an easier way of doing this. Is > there?
How many of these are you creating, one? Many? If many, are the associated with something? E.g. are they an inspector for an object, where you want to use the same window for the same object, or are they just freeform? No matter what, the best way is to keep a reference in a master controller. As usual, Apple provides a good design guide; NSDocumentController keeps a reference to all open documents, and NSDocument keeps a reference to all active NSWindowControllers. So then: - If it's just a single window, make an ivar to hold on to the controller. You don't even need to release it, since you can just reuse it. If you really want to, then have the window controller notify you when the window closes, then you can destroy it. - If you have multiple windows, store it in an NSSet (if they're just freeform) or an NSDictionary (if there's some sort of mapping). And as before, when the window closes, it should notify you so that you can remove the mapping. If you're not using garbage collection, you can play some tricks, like having the window controller retain itself, or having the app controller release the sender of the window-closed callback without actually keeping track of anything. But generally these only *appear* to save time, they don't actually help, so I don't recommend it. Mike _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]