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 (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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to