0、前言
- NSComboBox是Cocoa框架中的一个控件,用于在下拉列表中显示可选项,允许用户从中选择一个选项或输入自定义内容。
- 它可以用于许多场景,例如选择器、搜索框、标签选择等。
1、创建示例
复制
NSComboBox *combo_box = [[NSComboBox alloc] init];
[self.window.contentView addSubview:combo_box];
combo_box.frame = NSMakeRect(20, 400, 150, 30);
combo_box.hasVerticalScroller = NO;
combo_box.intercellSpacing = NSMakeSize(60, 20);
combo_box.itemHeight = 80;
combo_box.buttonBordered = YES;
combo_box.numberOfVisibleItems = 3;
NSInteger numberOfItems = combo_box.numberOfItems;
NSLog(@"用于获取下拉列表中项目的数量 == %@", @(numberOfItems));
combo_box.completes = NO;
NSInteger indexOfSelectedItem = combo_box.indexOfSelectedItem;
NSLog(@"用于获取当前选中的项目的索引值 == %@", @(indexOfSelectedItem));
id item_obj = [combo_box itemObjectValueAtIndex:2];
NSLog(@"item_obj == %@", item_obj);
id objectValueOfSelectedItem = combo_box.objectValueOfSelectedItem;
NSLog(@"用于获取当前选中的项目的值 == %@", objectValueOfSelectedItem);
NSInteger index = [combo_box indexOfItemWithObjectValue:@(23)];
NSLog(@"index == %@", @(index));
[combo_box selectItemAtIndex:2];
[combo_box deselectItemAtIndex:2];
[combo_box selectItemWithObjectValue:@(23)];
[combo_box scrollItemAtIndexToTop:2];
[combo_box scrollItemAtIndexToVisible:2];
[combo_box reloadData];
[combo_box noteNumberOfItemsChanged];
NSArray *objectValues = combo_box.objectValues;
NSLog(@"用于设置下拉列表中的项目值 == %@", objectValues);
combo_box.delegate = self;
#pragma mark - NSComboBoxDelegate
- (void)comboBoxWillPopUp:(NSNotification *)notification {
}
- (void)comboBoxWillDismiss:(NSNotification *)notification {
}
- (void)comboBoxSelectionDidChange:(NSNotification *)notification {
NSComboBox *comboBox = notification.object;
NSInteger selectedIndex = comboBox.indexOfSelectedItem;
NSLog(@"comboBoxSelectionDidChange selected item %@", self.datas[selectedIndex]);
}
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification {
NSComboBox *comboBox = notification.object;
NSInteger selectedIndex = comboBox.indexOfSelectedItem;
NSLog(@"comboBoxSelectionIsChanging selected item %@",self.datas[selectedIndex]);
}
-
1.6 效果

2、不使用数据源
combo_box.usesDataSource = NO;
[combo_box addItemWithObjectValue:@(23)];
[combo_box addItemsWithObjectValues:@[@(23), @"桂林"]];
[combo_box insertItemWithObjectValue:@(23) atIndex:2];
[combo_box removeItemWithObjectValue:@(23)];
[combo_box removeItemAtIndex:2];
[combo_box removeAllItems];
combo_box.target = self;
combo_box.action = @selector(comboBox_Selecte:);
- (void)comboBox_Selecte:(NSNotification *)notification {
NSComboBox *comboBox = [notification object];
NSInteger selectedIndex = [comboBox indexOfSelectedItem];
NSString *selectedItem = [comboBox itemObjectValueAtIndex:selectedIndex];
NSLog(@"Selected item: %@", selectedItem);
}
3、使用数据源
self.datas = @[@"小学", @"初中", @"高中",];
combo_box.usesDataSource = YES;
combo_box.dataSource = self;
#pragma mark - NSComboBoxDataSource
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
return self.datas.count;
}
- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
return self.datas[index];
}
- (NSUInteger)comboBox:(NSComboBox *)comboBox indexOfItemWithStringValue:(NSString *)string {
return 2;
}
- (nullable NSString *)comboBox:(NSComboBox *)comboBox completedString:(NSString *)string {
for (NSString *item in comboBox.objectValues) {
if ([item hasPrefix:string]) {
return item;
}
}
return nil;
}
4、API说明
APPKIT_EXTERN NSNotificationName NSComboBoxWillPopUpNotification;
APPKIT_EXTERN NSNotificationName NSComboBoxWillDismissNotification;
APPKIT_EXTERN NSNotificationName NSComboBoxSelectionDidChangeNotification;
APPKIT_EXTERN NSNotificationName NSComboBoxSelectionIsChangingNotification;
@protocol NSComboBoxDataSource <NSObject>
@optional
- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)comboBox NS_SWIFT_UI_ACTOR;
- (nullable id)comboBox:(NSComboBox *)comboBox objectValueForItemAtIndex:(NSInteger)index NS_SWIFT_UI_ACTOR;
- (NSUInteger)comboBox:(NSComboBox *)comboBox indexOfItemWithStringValue:(NSString *)string NS_SWIFT_UI_ACTOR;
- (nullable NSString *)comboBox:(NSComboBox *)comboBox completedString:(NSString *)string NS_SWIFT_UI_ACTOR;
@end
@protocol NSComboBoxDelegate <NSTextFieldDelegate>
@optional
- (void)comboBoxWillPopUp:(NSNotification *)notification NS_SWIFT_UI_ACTOR;
- (void)comboBoxWillDismiss:(NSNotification *)notification NS_SWIFT_UI_ACTOR;
- (void)comboBoxSelectionDidChange:(NSNotification *)notification NS_SWIFT_UI_ACTOR;
- (void)comboBoxSelectionIsChanging:(NSNotification *)notification NS_SWIFT_UI_ACTOR;
@end
@interface NSComboBox : NSTextField
@property BOOL hasVerticalScroller;
@property NSSize intercellSpacing;
@property CGFloat itemHeight;
@property NSInteger numberOfVisibleItems;
@property(getter=isButtonBordered) BOOL buttonBordered;
- (void)reloadData;
- (void)noteNumberOfItemsChanged;
@property BOOL usesDataSource;
- (void)scrollItemAtIndexToTop:(NSInteger)index;
- (void)scrollItemAtIndexToVisible:(NSInteger)index;
- (void)selectItemAtIndex:(NSInteger)index;
- (void)deselectItemAtIndex:(NSInteger)index;
@property(readonly) NSInteger indexOfSelectedItem;
@property(readonly) NSInteger numberOfItems;
@property BOOL completes;
@property(nullable, weak) id<NSComboBoxDelegate> delegate;
@property(nullable, assign) id<NSComboBoxDataSource> dataSource;
- (void)addItemWithObjectValue:(id)object;
- (void)addItemsWithObjectValues:(NSArray *)objects;
- (void)insertItemWithObjectValue:(id)object atIndex:(NSInteger)index;
- (void)removeItemWithObjectValue:(id)object;
- (void)removeItemAtIndex:(NSInteger)index;
- (void)removeAllItems;
- (void)selectItemWithObjectValue:(nullable id)object;
- (id)itemObjectValueAtIndex:(NSInteger)index;
@property(nullable, readonly, strong) id objectValueOfSelectedItem;
- (NSInteger)indexOfItemWithObjectValue:(id)object;
@property(readonly, copy) NSArray *objectValues;
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术