NSComboBox bound to NSArrayController not updating

417 views Asked by At

I have an issue with bindings between an NSComboBox element and an NSArrayController.

All bindings are setup in IB. The NSComboBox element has the following bindings:

  • Content: bound to the NSArrayController instance, key: arrangedObjects
  • Content Values: bound to the NSArrayController instance, key: arrangedObjects.name

The NSArrayController element is bound in the following way:

  • Content Array: bound to File's Owner, key path: availableProperties (which is an NSMutableArray

In the code, I have a method which is called when the window opens and after some event fires.

The code is the following:

NSMutableArray* newAvailable = ...; //compute the new properties to be displayed. 

//now I want to replace all the properties with the new one
if ([self.availableProperties count] > 0)
    [self.availablePropertiesController removeObjects:self.availableProperties];
[self.availablePropertiesController addObjects:newAvailables];

where self.availableProperties is the NSMutableArray (the model) and self.availablePropertiesController is the NSArrayController

When the window opens the combo box is properly populated. But when the event fires I execute the above statements, I can see the backing array correctly filled, but the combo box is completely empty.

Some ideas?

1

There are 1 answers

1
Wil Shipley On

You’re close, you should just do:

NSMutableArray* newAvailable = ...; //compute the new properties to be displayed. 
self.availableProperties = newAvailable;

You’ve already bound the arrayController’s to your ‘availableProperties’ variable, so all you have to do to change the UI is change your variable. That’s the beauty of bindings.

Also, your ‘availableProperties' should probably be an NSArray, not a NSMutableArray, because if you accidentally insert an object in the middle of the NSMutableArray the arrayController’s binding isn’t going to notice—it’ll only notice when the entire ‘availableProperties’ instance variable changes, not when something inside it mutates.