开发者

Objective-C Preferences Window makeKeyAndOrderFront or showWindow?

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!


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...


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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜