1

我有一个 NSButton,单击它时会打开一个 NSPopover,我唯一的问题是,当再次单击该按钮时,NSPopover 会在另一个按钮之上再次打开,弹出窗口使用以下命令打开:

- (IBAction)openSettingsPopover:(id)sender {
    NSViewController *controller = [[NSViewController alloc] initWithNibName:@"Settings" bundle:nil];
    NSPopover *popover = [[NSPopover alloc] init];
    [popover setContentSize:NSMakeSize(288.0f, 170.0f)];
    [popover setContentViewController:controller];
    [popover setAnimates:YES];
    [popover showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMaxXEdge];
}

如果用户在视图之外单击,我将如何关闭弹出窗口而不是打开另一个弹出窗口?提前致谢。

(左=单击一次,右=单击多次)

4

2 回答 2

3

Keep a reference to the NSPopover instance, check if it's not-nil before opening a new one

- (IBAction)openSettingsPopover:(id)sender {
    if (self.settingsPopover) {
        // Close it when clicked again, or simply return
        [self.settingsPopover close];
    }

    self.settingsPopover = [[NSPopover alloc] init];
    ...
}
于 2012-12-01T10:04:59.930 回答
2

一个更简单的方法是改变它的行为。只需将行为属性设置为 NSPopOverBehaviourTransient ,当用户与不在弹出框中的另一个 UI 元素交互时,它将被关闭。

[popover setBehaviour:NSPopOverBehaviourTransient];
于 2012-12-19T09:21:56.970 回答