Using NSUserDefaultsController with an NSPopUp button

  • 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".

    Any help, or pointers to good tutorials/books on bindings would be appreciated.

    Thanks,

    David
  • On Mon, 30 Oct 2006 14:29:52 +1100, "David Rappo" <david.rappo...>
    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 = <matt...>, <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>
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today