swiftnscombobox

comboBoxSelectionDidChange with multiple NScomboboxes


I have an app with two combo boxes in the same view that is both loaded from an SQLite database. If the user makes a selection in combo box 1, I want to fire a method to restrict the values in combo box 2.

I think I need comboBoxSelectionDidChange function, but I don't know how to tell whether the function has been fired by a selection in combo box 1 or 2?

Have looked at the function parameters but can't see how I can tell which combo box fired the function?


Solution

  • The notification contains an object which represents the NSComboBox instance.

    If you have created an NSComboBox outlet

    @IBOutlet weak var firstComboBox : NSComboBox!
    

    you can compare the instance with the notification object

    func comboBoxSelectionDidChange(_ notification: Notification) {
        let comboBox = notification.object as! NSComboBox
        if comboBox == firstComboBox {
          // do something with firstComboBox
        } else {
          // it's another comboBox
        }
    }