1、基础创建

  • 代码
// 创建
NSComboBox *combo_box = [[NSComboBox alloc] init];
// 位置尺寸
combo_box.frame = NSMakeRect(20, 200, 160, 35);
// 添加
[self.window.contentView addSubview:combo_box];
// 是否显示滚动条
combo_box.hasVerticalScroller = NO;
// 每个子项目的高度
combo_box.itemHeight = 40;
// 最多同时显示几个项目
combo_box.numberOfVisibleItems = 5;
// NSComboBox 输入框自己的边框是否显示
combo_box.buttonBordered = YES;
// 每个区块的间距
combo_box.intercellSpacing = NSMakeSize(0, 0);

combo_box.completes = YES;
// 设置代理
combo_box.delegate = self;
// 重载
[combo_box reloadData];

[combo_box noteNumberOfItemsChanged];

// 滚动指定项目到头部
[combo_box scrollItemAtIndexToTop:3];
// 滚动指定项目至可见
[combo_box scrollItemAtIndexToVisible:5];
// 选中哪个项目
[combo_box selectItemAtIndex:4];
// 取消选中项目
[combo_box deselectItemAtIndex:4];

// 选中的是第几个项目
NSInteger indexOfSelectedItem = combo_box.indexOfSelectedItem;
// 一共有几个项目
NSInteger numberOfItems = combo_box.numberOfItems;

#pragma mark - NSComboBoxDelegate
// 将要弹起
- (void)comboBoxWillPopUp:(NSNotification *)notification {
NSComboBox *combo_box = notification.object;

}

// 将要消失
- (void)comboBoxWillDismiss:(NSNotification *)notification {
NSComboBox *combo_box = notification.object;

}

// 已经选中一个项目
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
NSComboBox *combo_box = notification.object;
// 选中的是第几个项目
NSInteger indexOfSelectedItem = combo_box.indexOfSelectedItem;
// 一共有几个项目
NSInteger numberOfItems = combo_box.numberOfItems;

NSLog(@"选中的是第几个项目 == %@", @(indexOfSelectedItem));
NSLog(@"一共有几个项目 == %@", @(numberOfItems));
}

// 将要选中(按住一个项目,滚动滚轮依次划过)
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification {
NSComboBox *combo_box = notification.object;
// 将要选中第几个项目
NSInteger selectedIndex = combo_box.indexOfSelectedItem;
// 系统没有做防止越界,这里做处理
if (selectedIndex < 0 || selectedIndex > (self.datas.count - 1)) {
return;
}

NSLog(@"将要选中的项目 == %@", @(selectedIndex));
// 将要选中的项目
id selected_obj = self.datas[selectedIndex];
NSLog(@"将要选中的项目 == %@", selected_obj);
}
  • 效果
  • Mac开发_NSComboBox_数据

2、其他设置

2.1 使用数据源数据时的场景

// 是否使用数据源的数据
combo_box.usesDataSource = YES;
// 设置数据源
combo_box.dataSource = self;

// 以下两个字段仅在 不使用数据源数据的 情况下使用,即 usesDataSource 为NO 有效。
// 选中的项目
id objectValueOfSelectedItem = combo_box.objectValueOfSelectedItem;
// 所有项目
NSArray *objectValues = combo_box.objectValues;

#pragma mark - NSComboBoxDataSource
// 一共几个项目
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)comboBox {
return self.datas.count;
}

// 设置具体的数据
- (nullable id)comboBox:(NSComboBox *)comboBox objectValueForItemAtIndex:(NSInteger)index {
return self.datas[index];
}

// 开启时选中第几个项目
- (NSUInteger)comboBox:(NSComboBox *)comboBox indexOfItemWithStringValue:(NSString *)string {
return 2;
}

- (nullable NSString *)comboBox:(NSComboBox *)comboBox completedString:(NSString *)string {
NSLog(@"09876543 == %@", string);
return string;
}

2.2 不使用数据源数据时的场景

// 是否使用数据源的数据
combo_box.usesDataSource = NO;
// 以下两个字段仅在 不使用数据源数据的 情况下使用,即 usesDataSource 为 NO 有效。
// 选中的项目
id objectValueOfSelectedItem = combo_box.objectValueOfSelectedItem;
// 所有项目
NSArray *objectValues = combo_box.objectValues;
// 添加一个项目
[combo_box addItemWithObjectValue:@"10大学"];
// 添加多个项目
[combo_box addItemsWithObjectValues:@[@"10大学", @"11博士", @"12烟酒生"]];
// 插入一个项目
[combo_box insertItemWithObjectValue:@"10大学" atIndex:4];
// 移除一个项目
[combo_box removeItemWithObjectValue:@"10大学"];
// 移除一个项目
[combo_box removeItemAtIndex:3];
// 移除所有项目
[combo_box removeAllItems];
// 根据内容选中项目
[combo_box selectItemWithObjectValue:@"10大学"];
// 取出一个项目
id itemObject = [combo_box itemObjectValueAtIndex:3];
// 根据内容取出索引
NSInteger obj_index = [combo_box indexOfItemWithObjectValue:@"10大学"];


作者:​​ CH520​