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:
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 NSPopUpButton
are 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 content
property. In your case, you'd probably bind this to the arrangedObjects
key of an NSArrayController
. In this setup, each menu item represents one object. By default, the title of the menu item is the string returned by calling description
on each item in the array. If you want to use a different property for the menu title, you can also bind the contentValues
array. Just make sure the key path you specify for contentValues
has the key path for content
as its prefix (e.g. you might use arrangedObjects
for content
and arrangedObjects.name
for contentValues
)
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
, selectedObject
and selectedValue
. They represent, respectively, the index of the array object that the user selected, the object value (one of the objects in the content
array), and the string title of the selected item (one of the objects in the contentValues
array if you bound that property).
So, in your case, you might bind selectedObject
to a selectedSerialPort
property on your controller class. When the user clicks the "Connect" button, you only have to refer to the selectedSerialPort
property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With