View Full Version : Control NSArrayController Selection with NSPopUpButton?

Jan 23, 2008, 06:03 PM
Hi all,

I have an NSArrayController, set up in the usual way with the arrangedObjects displayed in a TableView. When I click on a row in the table view, the object selected becomes the array controllers selected object. Fine.
However, I would like to be able to control the array controllers selection from an NSPopUpBox. I have been able to bind the Content and Content Objects of the pop up button to show the arranged objects of the array controller, and it picks up the names of the objects perfectly well (using a model key path of name). However, when I select an item in the pop up button, the array controller selection does not change as the binding is one way.

Does anyone know how to get a popup button to control the selection of the controller? Any help will be greatly appreciated.

Jan 24, 2008, 12:23 PM
No-one can give me any pointers?

Jan 24, 2008, 12:41 PM
You could add an action to control the selection of the arrayController when the item in the popUpButton is selected.

Jan 25, 2008, 04:18 AM
Ah, I thought that's what I'd need to do. I just wanted to know if there was a way of doing it in bindings, but as NSPopUpButton is just a button I guess there isn't.

Jan 25, 2008, 10:04 AM
It may be possible to do it with bindings, but I don't know how...

Jan 25, 2008, 02:48 PM
In the end I did this:

[accountsController setSelectionIndex:[sender indexOfSelectedItem]];

Works like a charm. Thanks for the nudge in the right direction Eraserhead.

Jan 26, 2008, 11:53 PM
It can be done with bindings. It's not obvious though, but I found from the archives (http://www.cocoabuilder.com/archive/message/cocoa/2006/5/31/164724) what you need to do:

Bind the NSPopUpButton to the NSArrayController in three ways: its
content (to arrangedObjects), its contentValues (to arrangedObjects.name),
and its selectedIndex (to selectionIndex).

I initially tried with selectedObject instead of selectedIndex but I couldn't get that to work properly, not sure why. Here's an Xcode 3 project that demos this functionality.

Jan 27, 2008, 10:58 AM
That's excellent, thank you again Kainjow!