views:

196

answers:

2

Hello everybody,

I'm working on a new Mac App and want to open my Preferences Window, i've got 2 Nib (xib) Files, one for the main window, and one for the Preferences Window, then i've got a openPreferences Action, which shows the Preferences Window, sth. like this:

- (IBAction)openPreferences:(id)sender
{
    PrefCont *cont = [[PrefCont alloc] init];
    [cont showWindow:self];
}

this code works, but when i click more then once on the open Preferences Menu Item, then the Preferences Window opens twice or more then twice.

Is there a posibility to make it with sth. like makeKeyAndOrderFront but it must be called by the PrefController?

Or can i ask the Mac if the Window is opened? if not, then show it or sth. link this.

This would be very helpful, thanks to everbody!

+2  A: 

If you want to avoid the double window symptom, you should make PrefCont * cont an ivar of this class, and then do:

- (IBAction) openPreferences:(id)sender {
  if (cont == nil) {
    cont = [[PrefCont alloc] init];
  }
  [cont showWindow:sender];
}

This way you'll only be creating one preference controller, and tell that one to show its window.

Don't forget to [cont release]; when you're done...

Dave DeLong
thank you, this idea comes up to me yesterday night :D And its right, but thank you very much :)
ahmet2106
+2  A: 

A better way would be to have the PrefCont class have a singleton routine like:

+(PrefCont*)prefs
{
  static PrefCont* prefs = nil;
  if (!prefs)
     prefs = [[PrefCont alloc] init];

  return prefs;
}

and then whenever you want to show the preferences, just call

  [[PrefCont prefs] showWindow:sender];
Ken Aspeslagh