alwaysmpe
alwaysmpe

Reputation: 667

(Objective C) NSPopUpButton not updating displayed value

I'm making a UI using an NSPopUpButton, passing it an array of strings and selecting an item by index. When this is then shown, it defaults to the first item. Can anyone see what I'm missing? DropDownView extends an NSView.

@implementation DropDownView

- (id)initWithFrame: (NSRect)frameRect
             values: (NSArray<NSString*>*) vals
            current: (NSInteger) currentIndex
{
    [super initWithFrame:frameRect];
    menu = [[NSPopUpButton alloc]initWithFrame:frameRect pullsDown:NO];

    [menu addItemsWithTitles:vals];

    [menu setEnabled:true];

    // this does not change the currently displayed value
    // have tried various combinations of the below
    [menu selectItemAtIndex:currentIndex];
    // [menu selectItemWithTitle:vals[currentIndex]];
    [menu synchronizeTitleAndSelectedItem];

    [self addSubview:menu];

    [menu bind:@"selectedIndex" toObject:self withKeyPath:@"indexOfSelectedItem" options:nil];

    return self;
}
@end

many thanks!

Upvotes: 0

Views: 613

Answers (1)

Willeke
Willeke

Reputation: 15589

When you bind selectedIndex, the selectedIndex of menu is set to self.indexOfSelectedItem. Don't call selectItemAtIndex and synchronizeTitleAndSelectedItem and set indexOfSelectedItem to currentIndex.

Upvotes: 2

Related Questions