objective-cxcodecocoansarray

How do I bind an NSMutableArray to an NSPopUpButton


I have an NSPopUpButton defined in my view controller :

@property (weak) IBOutlet NSPopUpButton*    popUpButton;

this view controller also has an NSMutableArray which includes some objects of type NSDictionary, for instance:

NSDictionary *obj = @{@"name1": @"something",
                          @"name2": @"something else"};
    [myMutableArray addObject: obj];

I'd like my NSPopUpButton to reflect the contents of the name1 field of all elements contained in this array, but I have no idea how to achieve that with the bindings inspector in Xcode.

If I select the NSPopUpButton in the storyboard the following is shown in the bindings inspector:

Xcode bindings inspector

am I supposed to use "Contents" or "Content Values" to achieve of what I want?(assuming it's even possible). I've seen some people use an ArrayController but I'd like to know if it's possible to go about this without it.


Solution

  • You can achieve this by connecting the Content binding with myMutableArray and the Content Values binding with myMutableArray.name1.

    You may however run into another problem: adding objects to NSMutableArray does not fire key-value notifications, so if you add values after the UI did load those will not display.

    To work around this after modifications you can assign the mutable array to another instance variable and bind the UI to that array in an observable fashion:

    // modify array
    self.observedArray=myMutableArray;