0

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?

Francesco
  • 1,840
  • 19
  • 24

1 Answers1

0

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.

Wil Shipley
  • 9,343
  • 35
  • 59
  • I tried this and it does not work as I expected. First of all you can bound to a mutable collection. You just have to remember to either modify it only through the controller or by making sure your class is key value coding compliant for mutable to-many relationship. In my case I decided to use the controller, but I also tried to edit directly the collection and it does not work. BTW as I wrote changing the controller changes my array variable, simply it does not update the ui – Francesco Jan 11 '14 at 16:05