I’m exploring bindings right now, and have an NSPopUpButton –
It presents me a number of options for bindings under Value Selection – Content, Content Objects, Content Values, and then Selected Object, Selected Value, and Selected Tag. Could someone please explain the difference between these?
Those are explained in the Cocoa Bindings Reference for NSPopUpButton, although that reference is not quite clear.
Contentis an array controller that provides elements to the popup button. The array controller should be bound to an array. In order to determine how each element in the array is shown in the popup button,-descriptionis sent to each object in the array.You may customise this in two ways:
If you want the
Selected Objectbinding to provide an object distinct from the array elements managed by the array controller to whichContentwas bound, you can bindContent Objectsto another array controller. It could also be the same array controller but with a different key path;If you want the popup button options to be something different than the description of each element in the array managed by the array controller to which
Contentwas bound, you can bindContent Valuesto another array controller that manages an array whose elements contain the popup options. It could also be the same array controller but with a different key path.A simple example: suppose you have the following class:
and you haven’t overridden the
-descriptionmethod. In this case,-descriptionis useless and thenameproperty would be a good choice for the popup options. You’d bind:Contentto an array controller that manages an array ofCustomerinstances, controller keyarrangedObjects;Content Valuesto the same array controller, controller keyarrangedObjects, model keypathname.You can then bind
Selected Objectto something else, for example a property in your application delegate or window controller. Cocoa bindings would then assign the selectedCustomerinstance to that property.Now suppose you are not interested in the whole
Customerobject that’s been selected, but only its phone number. In this case, you can bindContent Objectsto the same array controller, controller keyarrangedObjects, model keypathphoneNumber. When a popup option is selected, Cocoa bindings will setphoneNumberinstead of an entireCustomerinstance. In summary: if you don’t bindContent Objects,Selected Objectrepresents the original object in the array. If you bindContent Objects, thenSelected Objectcan be something different.You’d bind
Selected Valueif you were not interested in the original objects (or the content objects), but the actual strings shown in the popup options according to theContent Valuesbindings.Quick recipe for providing data to the popup button:
Contentif you have objects (not only strings) that represent the popup options;Content Valuesif the options that are shown to the user cannot be obtained viaContentby sending-descriptionto the array elements;Content Objectsif you wantSelected Objectto return something different from the array elements fromContent.Quick recipe for obtaining the current selection in a popup button:
Selected Objectif you want to know the full object (either fromContentorContent Objects) representing the current popup selection;Selected Valueif you only want the string that’s currently selected in the popup.And lastly, you’d use
Selected Tagif the popup options are actually taken from a menu whose items have a tag set.