Control NSArrayController Selection with NSPopUpButton?

Discussion in 'Mac Programming' started by stadidas, Jan 23, 2008.

  1. macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #1
    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.
     
  2. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #2
    No-one can give me any pointers?
     
  3. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    You could add an action to control the selection of the arrayController when the item in the popUpButton is selected.
     
  4. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #4
    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.
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    It may be possible to do it with bindings, but I don't know how...
     
  6. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #6
    In the end I did this:

    Code:
    [accountsController setSelectionIndex:[sender indexOfSelectedItem]];
    
    Works like a charm. Thanks for the nudge in the right direction Eraserhead.
     
  7. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #7
    It can be done with bindings. It's not obvious though, but I found from the archives what you need to do:

    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.
     

    Attached Files:

  8. thread starter macrumors regular

    Joined:
    Feb 27, 2006
    Location:
    Kent, United Kingdom
    #8
    That's excellent, thank you again Kainjow!
     

Share This Page