Pergunta

Please help, this is driving me mad :(

I have a form as part of an OSX Cocoa app i'm writing which uses an NSPopUpButton to select the type of record you are creating/editing.

I am using bindings to bind the popup button to an array controller and my selected value is bound to an NSString.

I can get the popup populated and can select an item and save the record ok but there are two things I can't get right:

1) The selected value object contains the textual name of the item rather than its key - I have set Content Objects binding

2) I cannot restore the popup's selected item on reloading the form.

Here's some code samples to assist:

@implementation snippet:

IBOutlet NSPopUpButton *popMediaType;
IBOutlet NSArrayController *acMediaTypes;

@property (nonatomic, copy) NSString *selMediaType;

@interface snippet:

- (void)setupForm {
    NSArray *types = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Video", @"name", @"video", @"value", nil],
                                                       [NSDictionary dictionaryWithObjectsAndKeys:@"Advert", @"name", @"advert", @"value", nil],
                                                       [NSDictionary dictionaryWithObjectsAndKeys:@"Graphic", @"name", @"graphic", @"value", nil],
                                                       nil];

    [acMediaTypes setContent:types];
}

In interface builder:

acMediaTypes is the referencing outlet for an array controller object called mediaTypes popMediaType is the referencing outlet for the NSPopUpButton on the form

My NSPopUpButton's bindings is configured thus:

Content (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: <null>

Content Objects (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: value

Content Values (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: name

Selected Value (File's Owner.selMediaType)
Bind to: Files Owner
Controller key: <null>
Model Key Path: selMediaType

The popup displays correctly, shows the text from the "name" portion of the array properly.

If I read selMediaType in my code using something like NSLog() then I see "Video" for example if that item is selected - it should be "video" (lower case from the "value" part of the dictionary)

I can store the values in my sqlite database with no problems and inspect them using sqlite3 from the terminal and everything is saved.

If I load the values back from the table, I would have thought that just setting selMediatype would have reset the item position in the NSPopUpButton but it doesn't.

Foi útil?

Solução

Solution was easy in the end but not obvious to the new players to the game.

First, your recipient variable to bind the selection to must be a property not just an iVar

Second, in the bindings, bind selected object, NOT selected value.

Binding selected value will put the displayed valued in your target variable eg "Video" but binding to selected object will put the key in your target variable eg "video".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top