Search code examples
swiftmacoscocoaappkitnsbutton

NSButton does not call its action


I am having a little trouble with NSButton's action. I programmatically create a button ...

let continueButton = NSButton(title: "Continue", target: self, action: #selector(self.continueButtonClicked))

... to call this function:

@objc func continueButtonClicked() {
    print("continueButtonClicked")
}

However, continueButtonClicked is never called and the button appears to be disabled. I've tried enabling it, but that didn't do the trick.

Am I missing something?


Solution

  • Okay, it turns out, the button did call the action, but it only reacted after a few seconds of pressing on it. I managed to figure out that the reason for that was an NSClickGestureReckognizer that I had on the button's superview. Disabling it for the time the button is shown fixed the issue for me.

    I hope this helps someone experiencing similar issues.