Re: 'Pseudo' Singleton in Objective C
Re: 'Pseudo' Singleton in Objective C
- Subject: Re: 'Pseudo' Singleton in Objective C
- From: Pax <email@hidden>
- Date: Fri, 15 Mar 2013 07:43:41 +0000
Thank you to everyone whose helped me with this problem. I now have the solution I needed, and (as usual) I've learned a whole lot that will be useful in the future.
As always, this is an incredibly useful resource!
On 15 Mar 2013, at 02:16, Seth Willits <email@hidden> wrote:
> On Mar 14, 2013, at 11:22 AM, Pax wrote:
>
>> I don't really know how to describe what I'm trying to do except as a 'Pseudo' Singleton. I have a class with an NSWindow, which displays information. It is run by selecting an NSMenuItem called 'More Information…'
>>
>> My issue is that I only want one instance of the Information class to be loaded at a given time (and therefore only one information window on screen at a given time). If the information window is already loaded and it is then reselected from the menu then the existing window needs to be brought to the front - nothing more.
>>
>> I have currently resolved it by making my class a singleton…
>
> I don't see anything to "resolve" so for the sake of clarification…
>
> There's no reason to make your class a singleton just because you only want one instance of it. Just don't create a second instance if you already have one. Your "More Information" menu item is hooked up to some controller already — presumably the application delegate. All that controller has to do is maintain and reuse a reference to your InformationWindow.
>
>
>
>> … but that's not the right answer because it means that I can't release information window…
>
>
> Right. This is the reason you don't want a singleton or "pseudo" singleton. There's no fancy pattern needed:
>
>
> - (IBAction)showInformation:(id)sender
> {
> if (!_infoWindowController) {
> _infoWindowController = [[InformationWindowController alloc] init];
> [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(infoWindowWillClose:) name:NSWindowWillCloseNotification object:_infoWindowController.window];
> }
>
> [_infoWindowController showWindow:nil];
> }
>
>
> - (void)infoWindowWillClose:(NSNotification *)notification;
> {
> [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowWillCloseNotification object:_infoWindowController.window];
> [_infoWindowController autorelease];
> _infoWindowController = nil;
> }
>
>
> --
> Seth Willits
>
>
>
> _______________________________________________
>
> 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
_______________________________________________
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