I have an NSArrayController that I fill in the awakeFromNib method. The data has the keys: id, name, and description. I have a ComboBox and a TextField bound to the NSArrayController the first with the name and the second with the id. If I change the selection in the ComboBox I want the value in the TextField to change, and vice-versa. I read the docs for TextField and ComboBox bindings, but I didn’t understand how to achieve this.
I have an NSArrayController that I fill in the awakeFromNib method. The data has
Share
The trick here is that you need somewhere else to put the value of the NSComboBox. The NSArrayController is fine for providing the stock values to the NSComboBox, but you can type arbitrary values into an NSComboBox that might not be in the NSArrayController’s contentArray, so it’s not surprising that you need somewhere else to put the value. I was able to mock this up quickly by just putting a simple value on the AppDelegate like this:
Then in the implementation:
Then for the bindings, I set it up like this:
NSComboBox:
Continuously Updates Valueif you want the NSTextField to be updated letter-by-letter as you’re typing in the NSComboBox)NSTextField:
Continuously Updates Valueif you want the NSComboBox to be updated letter-by-letter as you’re typing in the NSTextField)If you want new values you type in to be added to the array, I’m sorry to say, that’s not readily doable with just these two controls and bindings. That’s a fair bit more complicated. But the trick for the simple case is that you need some place to store the value other than the NSArrayController you’re using to provide pre-loaded values to the NSComboBox.