8

我对可可很陌生,所以请原谅我犯的任何愚蠢的错误。我有一个 NSStatusItem,我想用它来打开一个菜单。但是,据我所知并听说过不同的表单,如果没有自定义视图,您只能使用弹出菜单。这是真的?如果是这样,您如何制作自定义视图来做某事(例如,在我的情况下打开一个窗口)?谢谢你的帮助。

4

1 回答 1

15

不,这不是真的。您需要为状态项设置目标和操作,以调用执行所需操作的方法(打开窗口)。

// This goes where you set up the status item
NSStatusItem *statusItem; // You need to get this from the status bar
[statusItem setTarget:self];
[statusItem setAction:@selector(openWindow:)];

// This method is called when the status item is clicked
- (void)openWindow:(id)sender {
    NSWindow *window = [self window]; // Get the window to open
    [window makeKeyAndOrderFront:nil];
}

您可能还想调用[NSApp activateIgnoringOtherApps:nil];您的 openWindow: 方法以确保您打开的窗口不在其他应用程序的窗口后面。

于 2011-03-26T19:58:39.393 回答