Предотвратить двойное открытие окна ... - PullRequest
1 голос
/ 13 мая 2011

Я выполняю эти строки, чтобы показать окно настроек:

-(IBAction)showPreferences:(id)sender {
    PreferencesWindowController *preferencesWindowController = [[PreferencesWindowController alloc] init];
    NSNib *preferencesNib = [[NSNib alloc] initWithNibNamed:@"PreferencesWindow" bundle:nil];
    [preferencesNib instantiateNibWithOwner:preferencesWindowController topLevelObjects:nil];
    [NSApp activateIgnoringOtherApps:YES];
    [[preferencesWindowController window] makeKeyAndOrderFront:nil]; 
    [preferencesNib release];
}

Но когда пользователь нажимает второй раз на кнопку настроек (и окно настроек все еще открыто), он открывает другой экземпляр окна настроек.

Как мне предотвратить это, не взламывая контрольные переменные? Должен ли я отредактировать мой PreferencesWindowController как одиночный?

1 Ответ

1 голос
/ 13 мая 2011

Мой подход заключается в том, чтобы сделать PreferencesWindowController ivar в любом классе, к которому принадлежит это действие:

@interface foo : NSObject
{
@private
  PreferencesWindowController *_pwc;
}
- (IBAction) showPreferencesWindow:(id)sender;
@end

@implementation foo

- (void) dealloc
{
  [_pwc release], _pwc = nil;
  [super dealloc];
}

- (IBAction) showPreferencesWindow:(id)sender
{
  if(nil == _pwc)
    _pwc = [[PreferencesWindowController alloc] initWithWindowNibName:@"PreferencesWindow"];
  [_pwc showWindow:sender];
}

@end
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...