NSPopUpButton with Fixed Values and Bindings

Posted on 29 September 2009 by Johannes Fahrenkrug. Tags: Programming Tutorials Cocoa
NSPopUpButton can be challenging to use. It would be quite desirable to have a very easy way to add an NSPopUpButton for fixed values, like "male" and "female". Unfortunately, that doesn't exist (at least not to my knowledge). That's why I'll show you really quick how to implement this with IB, bindings and some code.

  1. Create a plain old new Cocoa application in XCode and call it NSPopUpDemo.
  2. Open NSPopUpDemoAppDelegate.h, and add these two ivars:

    NSArray *genders;
    NSString *selectedGender;
    IBOutlet NSArrayController *arrayController;
    And add this property:

    @property (nonatomic, copy) NSString *selectedGender;

  3. Switch to NSPopUpDemoAppDelegate.m and add this at the top:

    @synthesize selectedGender;
    and add this to applicationDidFinishLaunching:

    genders = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"male", @"name", @"m", @"value", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"female", @"name", @"f", @"value", nil], nil];
    [arrayController setContent:genders];
  4. Open MainMenu.xib
  5. Drag an NSArrayController onto the nib and an NSPopUpButton onto the window.
  6. Control-drag from the App Delegate to the Array Controller and connect the arrayController outlet.
  7. Open the inspector for the NSPopUpButton. Bind it's content to Array Controller's arrangedObjects, it's content objects to Array Controller's arrangedObjects.value and it's content values to Array Controller's arrangedObjects.name:

  8. Build and Run, you should be able to select male or female.
  9. Open MainMenu.xib again and drag an NSTextField to the window. Bind it's value to the App Delegate's selectedGender:

  10. Also open the Inspector for the NSPopUpButton and bind it's selectedObject also to the App Delegate's selectedGender.
  11. Build and Run again. Now you should be able to enter "f" in the text field and the pop up should change to "female" and vice versa.
  12. And you're done!

Enjoy the bliss of KVC!


Nick said...

oop, nevermind, step 10, duh. Thanks again, you helped a lot

March 22, 2010 04:11 AM

Johannes Fahrenkrug said...

Hi Jakub,

no, I know you can do that. But the point of my post was to have a different value than the title that's displayed. I want the value to be "f" but the title in the popupbutton to be "female" and I want that to work with bindings. Unfortunately that doesn't seem to be possible to do in IB.

- Johannes

October 23, 2009 05:06 AM

Jakub Suder said...

Hmm... in Interface Builder, when you double-click on the popup button, you get a list of all options and you can edit or delete them. Also, if you look at the document window in hierarchical mode, you can expand the tree to see the popup button object and the options below it, then you can add new ones. Is this what you were looking for?

October 22, 2009 08:58 PM


Please keep it clean, everybody. Comments with profanity will be deleted.

blog comments powered by Disqus