NSPopUpButton, bindings and shorter lifespan

I am trying to understand how difficult it is to use NSPopUpButton. This is far and far the most complex user element for programming in Cocoa (at least as far as I find it).

The use case that I mean is as follows:

  • I have a Port class that represents a serial port.
  • Among the attributes is a name field.
  • In NSPopUpButton, I want to display a name field for each port.
  • When the user selects a specific port, it is marked with a checkmark in the pop-up window, as expected
  • When the user subsequently clicks the connect button, I can determine which of the ports from the array was selected.
  • I would like to achieve this using bindings, as I think, as soon as I get my head around, it will be a more elegant solution.

Therefore, in my AppController.h, I expect two attributes that 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]); } 

at Port.h I have

 NSString *name; NSString *baudRate; ... etc ... 

I created a simple project containing only a pop-up window (and a label) and following various articles, I managed to populate NSMutableArray with elements, which I then use an ArrayController to display the values, and then, when I select, set the label's value (using the object controller). However, as smart as it is, it is not suitable for the use that I am trying to implement. Therefore I seek help

M

+7
objective-c cocoa macos
source share
2 answers

OK, bindings with NSPopUpButton bit complicated because you need two things: a binding for values ​​and a binding for which one of these values ​​is selected. What makes it even more complicated is that there are a couple of completely legal ways to do this, and which one you choose depends entirely on your program structure and, to some extent, on personal preferences.

So, to get a list of values, you are binding the content property. In your case, you probably bind this to the arrangedObjects key of the NSArrayController . In this setting, each menu item represents one object. By default, the title of the menu item is the string returned by the description call for each element of the array. If you want to use another property for the menu name, you can also bind an array of contentValues . Just make sure the key path you specify for contentValues has a key path for content as its prefix (for example, you can use arrangedObjects for content and arrangedObjects.name for contentValues )

This will give you menu items that represent objects. Now you need to determine how to select 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 value of the object (one of the objects in the content array) and the row header of the selected element (one of the objects in the contentValues array, if you linked this property).

So, in your case, you can bind selectedObject to the selectedSerialPort property in your controller class. When the user clicks the Connect button, you only need to refer to the selectedSerialPort property.

+11
source share

You can download the sample Xcode project here . This applies as close to the description as possible.

It is important to monitor the connections between the array controller, AppController and the popup.

Basically, it just demonstrates what Alex said.

+6
source share

All Articles