gpt4 book ai didi

objective-c - NSComboBox 中的奇怪错误 : selectItemAtIndex does not work correctly when using data source

转载 作者:行者123 更新时间:2023-12-03 16:37:06 25 4
gpt4 key购买 nike

我在 NSComboBox 组件上遇到了一个奇怪的问题。其“selectIndexAtPath”行为根据数据源而变化:

  • “固定”列表会导致正确选择项目,但是当我通过单击右侧的箭头按钮打开列表时,它会一直被选中;
  • 使用数据源会导致正确选择该项目,但当我通过单击右侧的箭头按钮打开列表时,该项目仍会被选中 1/10 秒,但随后会被取消选择。

一些代码来说明:

@interface AppDelegate()

@property (weak) IBOutlet NSComboBox *combobox;
@property (strong, nonatomic) NSArray *temp;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.temp = @[@"Item", @"Item2", @"Item3", @"Item4", @"Item5"];

/* THIS DOES WORK */
self.combobox.usesDataSource = NO;
[self.combobox addItemsWithObjectValues:self.temp];

/* HOWEVER, THIS DOES NOT WORK */
self.combobox.usesDataSource = YES;
self.combobox.dataSource = self;

[self.combobox selectItemAtIndex:2];
}

#pragma mark - NSComboBoxDataSource methods

- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox
{
return self.temp.count;
}

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index
{
return self.temp[index];
}

有谁知道这是什么原因造成的吗?现在已经尝试了几天...谢谢!

最佳答案

找到了!

您还需要像这样实现indexOfItemWithStringValue:

- (NSUInteger)comboBox:(NSComboBox *)aComboBox indexOfItemWithStringValue:(NSString *)aString
{
return [self.temp indexOfObject:aString];
}

关于objective-c - NSComboBox 中的奇怪错误 : selectItemAtIndex does not work correctly when using data source,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17867020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com