action/value bound to NSPopupButton is only triggered when user changes the value from the UI. It doesnt get triggered when you change the value programmatically.
For example, if we bind the NSPopupButton's selected index property:
For example, if we bind the NSPopupButton's selected index property:
internal var selectedIndex: AnyObject! { didSet { let oldIndex = oldValue as? Int let newIndex = selectedIndex as? Int print("selectedIndex didSet, old: \(oldIndex), new: \(newIndex)") } }
changing the selected value via the UI will trigger selectedIndex.didSet as expected
However, when we change the index programmatically such as this:
popUpButton.selectItem(at: 2)
Doing so will change the value in the UI, but it won't trigger selectedIndex.didSet as expected.
A bigger issue happens when we have this scenario:
This happens because when we change the selected item programmatically to 2, selectedIndex's value doesn't actually change, it stayed at 1. So when the user change the index back to 1 via the UI, Cocoa realized that the value of selectedIndex is still the same (1), so it decided not to call the didset function.
A bigger issue happens when we have this scenario:
- User selects index 1 from the UI
- We programmatically call popupButton.selectItem(at: 2), the UI changed to show that index 2 is selected
- User reselects index 1 from the UI
- selectedIndex.didset is not called for the user action!!
This happens because when we change the selected item programmatically to 2, selectedIndex's value doesn't actually change, it stayed at 1. So when the user change the index back to 1 via the UI, Cocoa realized that the value of selectedIndex is still the same (1), so it decided not to call the didset function.