I am trying to come to grips with how difficult it is to use NSPopUpButton. It’s by far and away the most difficult user element to program in Cocoa (at least as far as I am finding it).
The use case I have in mind is as follows:
- I have a class called Port that represents a Serial port.
- Amongst the attributes is a name field.
- In the NSPopUpButton I want to display the name field for each port.
- When a user selects a specific port it is marked in the pop-up with a tick as expected
- When the user subsequently hits a connect button I can ascertain which of the Ports from the Array was selected.
- I would like to achieve this using bindings as I think once I get my head around it, it will be the more elegant solution.
Therefore in my AppController.h I am expecting two attributes which I can presumably create as properties and synthesize:
NSMutableArray *allPorts; Port *currentlySelectedPort;
and one action in my .m:
-(void)didSelectConnect:(id)sender{ NSLog(@'Selected port name is:%@',[currentlySelectedPort name]); }
in Port.h I have
NSString *name; NSString *baudRate; ... etc ...
I have created a simple project which contains just a pop up (and a label) and following various articles, I have managed to populate an NSMutableArray with elements which I then use an ArrayController to display values and then on selection set the value of a label (using an object controller). However, as much as this is clever it doesn’t fit the use case I am trying to implement. So I turn here for help
M
OK, bindings with the
NSPopUpButtonare a bit complicated because there are two things it needs: a binding for the values, and a binding for which one of those values is selected. What makes it even more complicated is that there are a couple of perfectly legitimate ways of doing that, and which one you choose entirely depends on your program’s structure and, to some extent, personal preferences.So, in order to get a list of values, you bind the
contentproperty. In your case, you’d probably bind this to thearrangedObjectskey of anNSArrayController. In this setup, each menu item represents one object. By default, the title of the menu item is the string returned by callingdescriptionon each item in the array. If you want to use a different property for the menu title, you can also bind thecontentValuesarray. Just make sure the key path you specify forcontentValueshas the key path forcontentas its prefix (e.g. you might usearrangedObjectsforcontentandarrangedObjects.nameforcontentValues)This will give you menu items that represent objects. What you need next is some way of identifying the selected one. There are three different bindings you can use:
selectedIndex,selectedObjectandselectedValue. They represent, respectively, the index of the array object that the user selected, the object value (one of the objects in thecontentarray), and the string title of the selected item (one of the objects in thecontentValuesarray if you bound that property).So, in your case, you might bind
selectedObjectto aselectedSerialPortproperty on your controller class. When the user clicks the ‘Connect’ button, you only have to refer to theselectedSerialPortproperty.