Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPopUpButton, Bindings and a shortening lifespan

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

like image 902
Clokey Avatar asked Apr 10 '09 14:04

Clokey


1 Answers

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.

like image 144
Alex Avatar answered Sep 30 '22 19:09

Alex