Luk*_*uke 7 cocoa window menu objective-c nsstatusitem
我对可可很新,所以请原谅我犯的任何愚蠢错误.我有一个NSStatusItem,我想用它来打开菜单.但据我所知并且已经听过不同的形式,如果没有自定义视图,您只能使用弹出菜单.这是真的?如果是这样,你怎么做一个自定义视图来做某事(例如在我的情况下打开一个窗口)?谢谢你的帮助.
ugh*_*fhw 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];
}
Run Code Online (Sandbox Code Playgroud)
您可能还想调用[NSApp activateIgnoringOtherApps:nil];openWindow:方法来确保您打开的窗口不在其他应用程序窗口的后面.