Re: Binding a NSArrayController to a NSPopupButtonn & NSTextField
Re: Binding a NSArrayController to a NSPopupButtonn & NSTextField
- Subject: Re: Binding a NSArrayController to a NSPopupButtonn & NSTextField
- From: John Joyce <email@hidden>
- Date: Mon, 18 Feb 2013 00:24:09 +0900
On Feb 16, 2013, at 1:21 AM, Keary Suska <email@hidden> wrote:
> On Feb 15, 2013, at 5:41 AM, Eric Gorr wrote:
>
>> I have a NSArrayController filled with an array of NSDictionaries.
>>
>> [[self controller] addObject:@{ @"name" : @"itemA", @"part" : @"partA" }];
>> [[self controller] addObject:@{ @"name" : @"itemB", @"part" : @"partB" }];
>> [[self controller] addObject:@{ @"name" : @"itemC", @"part" : @"partC" }];
>>
>> I am populating a NSPopupButton with the items in this array based on the 'name' key. This is easily accomplished with the following bindings.
>>
>> Content Array binding -
>> bound to: Array Controller
>> controller key: arrangedObjects
>>
>> Content Values binding
>> bound to: array controller
>> controller key: arrangedObjects
>> model key path: name
>>
>> I would then like to populate a NSTextField with the text in the 'part' key based on the current selection of the NSPopupButton. I have setup the following binding:
>>
>> Value binding
>> bound to: array controller
>> controller key: selection
>> model key path: part
>>
>> With these bindings alone, the text field does display 'partC'.
>>
>> However, if I change the value of the NSPopupMenu, what the text field shows does not change.
>
> It is useful to note that even though an object will bind to an NSArrayController, it does not mean that it will employ the controller's selection semantics. NSPopupButton is one of them, I believe. The "proper" way to do this is bind the NSPopupButton's selectedObject/Value to a controller that maintains the value (*not* the array controller), and then bind the NSTextField to that other controller. You may be able to use an NSObjectController in the xib to act as the "proxy" for the value, however kludgey.
>
> HTH,
>
> Keary Suska
> Esoteritech, Inc.
> "Demystifying technology for your home or business"
>
On the contrary, this totally can work.
Here's how… (a tutorialized version of the OP's originally linked test app with corrections)
Binding NSArrayController to NSPopUpButton
In your AppDelegate.h file, add a property for an NSArray.
@property (strong) NSArray *anArray;
In the AppDelegate.m file, in the applicationDidFinishLaunching: method, create an NSArray of NSDictionary objects.
(using NSArray and NSDictionary literals here for convenience)
_anArray = @[ @{@"aKey" : @"Value for aKey", @"anotherKey in dict1" : @"Value for anotherKey in dict1"},
@{@"aKey" : @"Value for aKey", @"anotherKey in dict2" : @"Value for anotherKey in dict2"},
@{@"aKey" : @"Value for aKey", @"anotherKey in dict3" : @"Value for anotherKey in dict3"},
@{@"aKey" : @"Value for aKey", @"anotherKey in dict4" : @"Value for anotherKey in dict4"}];
In xib file, add one NSPopUpButton, one NSTextField label and one NSArrayController.
The NSArrayController Bindings
Select the NSArrayController object in the xib, then select the Bindings Inspector.
In the Bindings Inspector, click on Content Array.
For Bind to, select App Delegate from the popupbutton menu.
For the field Model Key Path , enter self.anArray to bind the NSArrayController to the NSArray anArray in the AppDelegate.
The NSPopUpButton Bindings
Select the NSPopUpButton object in the xib, then select the Bindings Inspector.
In the Bindings Inspector, click on Content .
For Bind to, select Array Controller from the Bind to PopUpButton menu.
For the field Controller Key , enter arrangedObjects.
This sets the data source, if you will, for the NSPopUpButton in your xib.
In the Bindings Inspector, click on Content Values .
For Bind to, select Array Controller from the Bind to PopUpButton menu.
For the field Controler Key , enter arrangedObjects.
For the Model Key Path, enter the key name for the values from your dictionaries that you want to display in your NSPopUpButton menu.
In this case, enter aKey.
This determines what values from your data source objects are displayed in your NSPopUpButton menu.
In the Bindings Inspector, click on Selected Index .
For Bind to, select Array Controller from the Bind to PopUpButton menu.
For the field Controler Key , enter selectionIndex.
This will update the Array Controller's property selectionIndex to the index of the object selected in the NSPopUpButton.
The NSTextField Bindings
Select the NSTextField object in the xib, then select the Bindings Inspector.
In the Bindings Inspector, click on Value.
For Bind to, select Array Controller from the Bind to PopUpButton menu.
For the field Controler Key , enter selection.
For the Model Key Path, enter the key name for the values from your dictionaries that you want to display in your NSTextField.
In this case, enter anotherKey.
This will display the value for anotherKey from the dictionary object that is selected in the NSPopUpButton.
Save, build and run.
Clicking the NSPopUpButton will display the value for the designated key for for the dictionary for each item in the NSPopUpButton
s associated NSMenu.
Selecting an item will then cause the NSArrayController to update its selectionIndex, the index of the selected object.
This in turn will cause the NSTextField to display the value for the designated key for the NSDictionary object corresponding to the selection in the NSPopUpButton.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden