开发者

Open and close NSWindow with NSButton through NSWindowController?

开发者 https://www.devze.com 2023-04-10 23:58 出处:网络
I\'ve got a custom NSButton that I\'m actually sticking in an NSStatusItem. When the NSButton is clicked, it launches my window. When the 开发者_如何学PythonNSButton is clicked again, the window shoul

I've got a custom NSButton that I'm actually sticking in an NSStatusItem. When the NSButton is clicked, it launches my window. When the 开发者_如何学PythonNSButton is clicked again, the window should close.

If the window is open, it appears as if the NSButton stops responding (or doesn't receive) click events! Here's the relevant code:

[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];
}

Am I doing something crazy? If I set a breakpoint in the above, it is hit when the button is clicked the first time but is not hit when the button is clicked again.


I'm guessing that you're not setting a target on the button. If a button has an action but no target, it gets sent up the responder chain. When the new window is shown, the responder chain is probably being changed, which means your action is being sent to a different place.

tl;dr: try setting a target on the button.

0

精彩评论

暂无评论...
验证码 换一张
取 消