0
我有一個自定義NSButton,我實際上粘在一個NSStatusItem中。當NSButton被點擊時,它會啓動我的窗口。當再次單擊NSButton時,窗口應該關閉。通過NSWindowController通過NSButton打開和關閉NSWindow?
如果窗口打開,它看起來好像NSButton停止響應(或不接收)單擊事件!這裏的相關代碼:
[statusItem setView:myCustomButton];
[myCustomButton setAction:@selector(showWindow:)];
- (void)showWindow:(id)sender {
if(!myWindowController) {
myWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
}
[myWindowController showWindow:statusItem];
[myWindowController.window orderFront:nil];
}
我在做一些瘋狂的事情嗎?如果我在上面設置了一個斷點,它會在第一次點擊按鈕時被點擊,但是當再次點擊該按鈕時沒有點擊。
這聽起來似乎合理。 -showWindow:在NSWindowController上實現,因此每個帶有窗口控制器的窗口都可以攔截該消息。但是,由於包含按鈕的窗口始終位於最前面的窗口中,因此窗口控制器應始終攔截該消息。實際上,由於在該方法中正在分配窗口控制器,因此該窗口控制器會截斷該消息,因此它總是顯示該窗口。將方法簽名更改爲「toggleWindow:」,看看會發生什麼。 –
做到了!多謝你們。 –