如何在单击NSStatusItem时打开一个窗口?

时间:2011-03-26 16:20:54

标签: objective-c cocoa menu window nsstatusitem

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

1 个答案:

答案 0 :(得分:14)

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

// 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:方法,以确保您打开的窗口不在其他应用程序窗口的后面。