Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
You could add an action to control the selection of the arrayController when the item in the popUpButton is selected.
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
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.
 

stadidas

macrumors regular
Original poster
Feb 27, 2006
243
0
Kent, United Kingdom
In the end I did this:

Code:
[accountsController setSelectionIndex:[sender indexOfSelectedItem]];

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

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
It can be done with bindings. It's not obvious though, but I found from the archives 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.
 

Attachments

  • NSPopUpButtonBindings.zip
    64.2 KB · Views: 972
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.