如何只打开一个窗口"首选项"?

Nik*_*nyi 1 macos objective-c

我的应用有首选项窗口.我用这个代码打开它

- (IBAction)openPreferences:(id)sender {

    NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"Preferences"];
    [windowController window];
}
Run Code Online (Sandbox Code Playgroud)

如果再次按下命令,则会一次又一次地再次打开新的"首选项"窗口 - 一次又一次......

怎么只打开一个窗口?谢谢!

Nat*_*Day 6

使windowController成为AppDelegate的实例变量,并将打开的首选项更改为

- (IBAction)openPreferences:(id)sender
{
    if( windowController == nil )
        windowController = [[NSWindowController alloc] initWithWindowNibName:@"Preferences"];
    [windowController showWindow:sender];
}
Run Code Online (Sandbox Code Playgroud)