Re: Using NSUserDefaultsController with an NSPopUp button
Re: Using NSUserDefaultsController with an NSPopUp button
- Subject: Re: Using NSUserDefaultsController with an NSPopUp button
- From: Matt Neuburg <email@hidden>
- Date: Sun, 29 Oct 2006 21:12:19 -0800
- Thread-topic: Using NSUserDefaultsController with an NSPopUp button
On Mon, 30 Oct 2006 14:29:52 +1100, "David Rappo" <email@hidden>
said:
>Hi,
>
>I am experiencing problems using NSUserDefaultsController with an
>NSPopUp button, and an NSArray of strings (the source array for the
>NSPopUp button). The goal is to allow the user to add/remove strings
>from the NSPopUp button, and for the program to remember the state of
>the NSPopUp after the user quits and restarts.
>
>My bindings are setup as follows:
>
>* NSArrayController called colorArray
>
>Its contentArray is bound to the NSUserDefaultsController with
>controller key = values, and model key path = "colorArrayKey" (the key
>for my array in the NSUserDefaults object)
>
>The object class name is set to NSString.
>
>* NSPopUpButton
>
>Its contentValues are bound to the colorArray controller with
>controller key = arranged objects. Model key path is empty
>
>The selectedIndex is also bound to the colorArray controller with
>controller key = selectionIndex. Model key path is empty.
>
>So, as I understand things, I've provided the NSArrayController with
>the key (colorArrayKey) for an array of strings in NSUserDefaults,
>which it should use to populate the NSPopUp button.
>
>When I run the app, the NSPopUp button on the screen contains a single
>entry called "No Value".
There are two issues here.
(1) To hook an NSPopUpButton to an NSArrayController, read this:
<http://www.cocoabuilder.com/archive/message/cocoa/2006/5/31/164724>
(2) The binding to NSUserDefaultsController must not be made until after the
app gets going. The reason is that otherwise there's a timing problem
(things are hooking up in the wrong order). So, don't bind the
NSUserDefaultsController in Interface Builder at all; do it in code after
the interface has sprung to life, or better still don't bother just read
the information back and forth between user defaults and the popup menu
yourself, in code, as necessary. Take a look at my app (NotLight); you'll
see that I'm doing exactly what you're after. The popup menu in the window
is user-configurable and the added items are remembered the next time you
run the app. What I actually do is this (in my main controller's
awakeFromNib):
[self->searchTypesPopupController setContent: // blah blah];
[self->searchTypesPopupController bind:@"selectionIndexes"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.aPopupSelection"
options:[NSDictionary dictionaryWithObject:
[[[MyNumberSetTransformer alloc] init] autorelease]
forKey: NSValueTransformerBindingOption]];
So you see I actually populate the popup menu (from user defaults) by hand
as we start up. And only then do I bind the popup menu's selection to
NSUserDefaults, because otherwise you get that "No Value" you're talking
about.
m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide - Second Edition!
<http://www.amazon.com/gp/product/0596102119>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden