gpt4 book ai didi

objective-c - 防止 window 两次打开...

转载 作者:行者123 更新时间:2023-12-03 17:16:12 27 4
gpt4 key购买 nike

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

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

最佳答案

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

关于objective-c - 防止 window 两次打开...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991585/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com