Setting Programmatic Actions for NSPopUpButton

306 Views Asked by At

This is a very simple... confusing thought question. I'm a bit confused as to how calls work with NSObjects, and I can't seem to get this to work at all (despite being... what I assume, very simple).

I've create an NSViewController with a matching .xib. In the XIB is a label, a pop up button, and a regular button, shown below.

enter image description here

All I want, is for the button to reflect the selected number of the Pop Up Button. Easy enough to set up the first time in code, in the View Controller.

class Root: NSViewController {
    //MARK: IBs
    @IBOutlet weak var noOfImages: NSPopUpButton!
    @IBOutlet weak var genButton: NSButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        genButton.title = "Generate \(noOfImages.selectedItem?.title) Image"
        noOfImages.autoenablesItems = false

        //Adding target
        noOfImages.target = self
        noOfImages.action = #selector(imageValueChanged(_:))
    }

    @objc func imageValueChanged(_ sender: NSPopUpButton) {
        print("print changed")
    }

}

Or so I think. My imageValueChanged function is never called. Ever. I've also tried changing the @objc func to a simple IBAction, and still - nothing.

Am I missing something glaringly obvious about getting an action to actually register? Is this a bug?

EDITED: Just to add - I tried connecting an action and selector to just the NSButton as well. Nothing. When I sendAction with the button, however, I get a response. I really have zero idea what is going on.

1

There are 1 best solutions below

0
Misha Stone On

In the AppDelegate, be sure to render contentViewController and not contentView.