[英]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.