简体   繁体   中英

Binding a NSComboBox to an NSArrayController programmatically

I can't populate a NSComboBox . I'm trying to bind it programmatically to an NSArrayController :

frequencyArrayController = [[NSArrayController alloc] initWithContent:nil];
[frequencyArrayController setManagedObjectContext:[[NSApp mainWindowDocument] managedObjectContext]];
[frequencyArrayController setEntityName:@"Frequency"];
[frequencyArrayController fetch:self];
[frequencyComboBox bind:@"contentValues" toObject:frequencyArrayController withKeyPath:@"arrangedObjects.DisplayName" options:nil];

What am I missing?

The field is on a NSPanel which is not open yet when the app starts. I'm binding it and loading the nib in advance, is this an issue?

You need to tell your array controller to fetch: at some point.

Edit: Here's the example code I'm using to test:

    arrayController = [[NSArrayController alloc] init];
    [arrayController setManagedObjectContext:self.managedObjectContext];
    [arrayController setEntityName:@"Entity"];
    [comboBox bind:@"contentValues" toObject:arrayController         
        withKeyPath:@"arrangedObjects.name" options:nil];
    [arrayController fetch:self];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
       NSLog(@"%@", comboBox.objectValues); 
    }];

Check that your MOC and IBOutlets are non-nil.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM