防止窗户两次打开ߪ

发布于 2024-11-07 01:10:41 字数 658 浏览 0 评论 0原文

我执行这些行来显示首选项窗口:

-(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 编辑为单例吗?

I execute these lines to show the preferences window:

-(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];
}

But when the user clicks a second time on the preferences button (and the preferences window is still open) it will open up another instance of the preferences window.

How should I prevent this without hacking around with control variables? Should I edit my PreferencesWindowController to be a singleton?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

圈圈圆圆圈圈 2024-11-14 01:10:41

我的方法是在该操作所属的任何类中创建一个 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

My approach would be to make a PreferencesWindowController ivar in whatever class this action belongs to:

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文