- Create a plain old new Cocoa application in XCode and call it NSPopUpDemo.
- Open NSPopUpDemoAppDelegate.h, and add these two ivars:
And add this property:NSArray *genders;NSString *selectedGender;IBOutlet NSArrayController *arrayController;
@property (nonatomic, copy) NSString *selectedGender;
- Switch to NSPopUpDemoAppDelegate.m and add this at the top:
and add this to applicationDidFinishLaunching:@synthesize selectedGender;
genders = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"male", @"name", @"m", @"value", nil],[NSDictionary dictionaryWithObjectsAndKeys:@"female", @"name", @"f", @"value", nil], nil];[arrayController setContent:genders]; - Open MainMenu.xib
- Drag an NSArrayController onto the nib and an NSPopUpButton onto the window.
- Control-drag from the App Delegate to the Array Controller and connect the arrayController outlet.
- 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:
- Build and Run, you should be able to select male or female.
- Open MainMenu.xib again and drag an NSTextField to the window. Bind it's value to the App Delegate's selectedGender:
- Also open the Inspector for the NSPopUpButton and bind it's selectedObject also to the App Delegate's selectedGender.
- 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.
- And you're done!
Enjoy the bliss of KVC!
Comments
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
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
Nick said...
oop, nevermind, step 10, duh. Thanks again, you helped a lot
March 22, 2010 04:11 AM