Search code examples
objective-ccocoacocoa-bindingsnsarraycontroller

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?


Solution

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