(目标C)NSPopUpButton不更新显示值

时间:2016-03-10 18:16:09

标签: objective-c macos nsview nspopupbutton

我正在使用NSPopUpButton创建一个UI,传递一个字符串数组并按索引选择一个项目。如果显示,则默认为第一个项目。任何人都能看到我错过的东西吗? DropDownView扩展了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
非常感谢!

1 个答案:

答案 0 :(得分:1)

绑定selectedIndex时,菜单的selectedIndex设置为self.indexOfSelectedItem。不要调用selectItemAtIndex和synchronizeTitleAndSelectedItem并将indexOfSelectedItem设置为currentIndex。