Binding NSPopupButton to NSDictionaryController

I am trying to run some MacOS programs and have some problems understanding how bindings work with NSPopupButton. I'm interested in binding to the NSDictionaryController (I don't think I need an intermediate NSArrayController, but if this is the best way, I'm open to it).

I created a controller object that has the 'db' property, which has the 'species' property, which is NSMutableDictionary. The "species" dictionary has an identifier for keys and species objects for values. View objects have a description property. In InterfaceBuilder, I created MyController, NSDictionaryController, and NSPopupButton. I would like to pop up the Species.descriptions popup. When selected, I need access to the corresponding identifier.

I installed NSDictionaryController to associate the "Content Dictionary" with MyController with the Key Path pattern "db.species". With NSPopupButton so far, I have bound the "Content Values" to the NSDictionaryController with the controller key "builtObjects" and the Path Key Path set to "value.description".

This seems to work by filling out the list. My main question is the best way to connect the choice. Ideally, I would like to connect the selection to an NSDictionaryController so that I can use the NSDictionaryController to access the selection. One reason for this is that I can connect other controls to the NSDictionaryController to see the current selection. If not, should I connect to a property in MyController or something else? Just look for best practices. I would like it to be through Interface Builder mechanisms so that I can easily reuse the model and design of the controller in another application with a different view.

Update using Brian's answer as a guide:

NSPopupButton: associate content with NSDictionaryController-> orderedObjects-> value.description

bind content objects to NSDictionaryController-> assemblyObjects-> key

bind selected pointer to NSDictionaryController-> selectionIndex

bind NSDictionaryController-> db.species

Everything works. I can capture an object from the controller using the value [[[selected selectedbjects] lastObject]]. I think this is in an array of selected objects with keys, pairs of values.

+5
source share
2 answers

I have never tried this with the NSDictionaryController, but I think you want to bind the contentObjects for the popup to the dict controller "assemblyObjects.key" and bind the selectedObject to the dict selection key. The contentObjects binding will indicate the identifiers as the underlying objects represented by each menu item. Then, when an item is selected from a pop-up window, the binding of the selected object binds the identifier corresponding to this menu item, like the choice of a dict controller.

+3
source

I would like to pop up a popup with Species.description. When selected, I need access to the corresponding identifier.

Bind content in the dictionary controller arrangedObjects.value (does not include description -The pop-up button will do this for you) and contentObjects in the dictionary controller arrangedObjects.key .

For more information, see NSPopUpButton in Cocoa Link .

(I noticed that he describes content as β€œan instance of NSArrayController ...". Dictionary controllers are array controllers, so this should not be a problem, but there may be a binding to the controller property. Something to pay attention to.)

+2
source

All Articles