0

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?

Reza Mousavi
  • 4,420
  • 5
  • 31
  • 48
Colin W
  • 45
  • 2
  • 7

2 Answers2

0

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
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

The solution for ascertaining which combobox fired the selectionDidChange method worked fine. One thing I did learn though was that the method is fired before .stringValue for the new selection is updated. I wanted to use the new .stringValue in the selectionDidChange method but discovered the previous .stringValue.

The way I got around this was to use the indexOfSelectedItem which is updated before the selectionDidChange method is fired.

So my code became

//In order to be able to restrict the contacts available when a company is selected use the comboboSelectionDidChange function func comboBoxSelectionDidChange(_ notification: Notification) {

    //Define a constant combobox that takes the notification object and casts it as an NSCombobox
    let combobox = notification.object as! NSComboBox

    //Can now test on the combobox object because we only want to do something if cmbCompany selection changes
    if combobox == cmbCompany {

        //The issue with comboboSelectionDidChange function is that the method is fired before the .stringValue is updated. So using .stringValue wont work as it will use the .stringValue from previous selected item. Therefore use index of       selected item as this is updated before the comboboSelectionDidChange function is fired

        //Define index which is the index of the newly selected item
        let index = cmbCompany.indexOfSelectedItem

        //Because the compIndex index will be the same as the index of the selected item we can get the newly selected company from the compIndex which is used to populate the cmbCombobox
        let company = compIndex[index]

        //Get the idCompany from combobox selection passing in the company from the new selection
        idcom = (delegate as! ViewController).getPrimaryKeyComp(company: company)

        //Call the function to reload the contIndex with only contacts for the company selected
        contIndex = (delegate as!ViewController).getContactForCompany(ic: idcom)

        cmbContact.reloadData()

    }
}
Colin W
  • 45
  • 2
  • 7