繁体   English   中英

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

[英]How to open a window on click on NSStatusItem?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM