OS X开发:下拉菜单按钮NSPopUpButton应用
2017-07-24 00:00
691 查看
摘要: NSPopUpButton是Mac软件中常用的下拉列表按钮。
效果如下图所示:
NSPopUpButton继承与NSButton,因此NSButton添加触发事件的方式在NSPopUpButton中依然使用,NSPopUpButton类中属性和方法解析如下:
OS X开发:下拉菜单按钮NSPopUpButton应用
NSPopUpButton是一个下拉按钮,当用户点击时,其会弹出一个下拉选择菜单。一个简单的示例如下:- (void)viewDidLoad { [super viewDidLoad]; NSPopUpButton * popUpButton = [[NSPopUpButton alloc]initWithFrame:CGRectMake(100, 400, 200, 300)]; //设置弹出菜单 NSMenu * menu = [[NSMenu alloc]initWithTitle:@"menu"]; [menu insertItemWithTitle:@"one" action:@selector(null) keyEquivalent:@"" atIndex:0]; [menu addItemWithTitle:@"two" action:@selector(null) keyEquivalent:@""]; popUpButton.menu = menu; //设置弹出菜单的位置 popUpButton.preferredEdge = NSRectEdgeMaxX; [self.view addSubview:popUpButton]; }
效果如下图所示:
NSPopUpButton继承与NSButton,因此NSButton添加触发事件的方式在NSPopUpButton中依然使用,NSPopUpButton类中属性和方法解析如下:
//初始化方法 flag参数决定是下拉菜单模式还是弹出菜单模式 - (instancetype)initWithFrame:(NSRect)buttonFrame pullsDown:(BOOL)flag; //设置下拉菜单 @property (nullable, strong) NSMenu *menu; //设置当交互事件发生时,是否禁用选项 @property BOOL autoenablesItems; //风格设置是否为下拉菜单 @property BOOL pullsDown; //设置菜单弹出的优先位置 @property NSRectEdge preferredEdge; //列表按钮相关 //添加一个按钮 - (void)addItemsWithTitles:(NSArray<NSString *> *)itemTitles; //插入一个按钮 - (void)insertItemWithTitle:(NSString *)title atIndex:(NSInteger)index; //通过标题移除一个按钮 - (void)removeItemWithTitle:(NSString *)title; //通过索引移除按钮 - (void)removeItemAtIndex:(NSInteger)index; //移除所有按钮 - (void)removeAllItems; //所有列表选项按钮数组 @property (readonly, copy) NSArray<NSMenuItem *> *itemArray; //按钮个数 @property (readonly) NSInteger numberOfItems; //获取按钮索引的方法 - (NSInteger)indexOfItem:(NSMenuItem *)item; - (NSInteger)indexOfItemWithTitle:(NSString *)title; - (NSInteger)indexOfItemWithTag:(NSInteger)tag; - (NSInteger)indexOfItemWithRepresentedObject:(nullable id)obj; - (NSInteger)indexOfItemWithTarget:(nullable id)target andAction:(nullable SEL)actionSelector; //获取按钮的方法 - (nullable NSMenuItem *)itemAtIndex:(NSInteger)index; - (nullable NSMenuItem *)itemWithTitle:(NSString *)title; //获取最后一个按钮 @property (nullable, readonly, strong) NSMenuItem *lastItem; //选择某个按钮的方法 - (void)selectItem:(nullable NSMenuItem *)item; - (void)selectItemAtIndex:(NSInteger)index; - (void)selectItemWithTitle:(NSString *)title; - (BOOL)selectItemWithTag:(NSInteger)tag; - (void)setTitle:(NSString *)string; //获取选中的按钮 @property (nullable, readonly, strong) NSMenuItem *selectedItem; //获取已经选中的按钮索引 @property (readonly) NSInteger indexOfSelectedItem; //获取已经选中的按钮tag @property (readonly) NSInteger selectedTag; //将选中的标题显示进行同步 - (void)synchronizeTitleAndSelectedItem; //获取某个索引按钮的标题 - (NSString *)itemTitleAtIndex:(NSInteger)index; //获取按钮标题数组 @property (readonly, copy) NSArray<NSString *> *itemTitles; //获取选中的按钮标题 @property (nullable, readonly, copy) NSString *titleOfSelectedItem; //当下拉菜单弹出时发送的通知 APPKIT_EXTERN NSNotificationName NSPopUpButtonWillPopUpNotification;
相关文章推荐
- os x开发之隐藏按钮
- 关于Eclipse插件开发(四)-------给视图加下拉菜单和按钮和加入编辑器.
- 表单开发(二):获取单选按钮,多行文本框,下拉菜单,捆绑元素数据,用户注册
- OS X开发:NSButton按钮控件应用
- 安卓开发中给按钮(button)设置监听器的4种方法
- Android开发笔记--按钮添加事件的四种方法
- JAVA安卓开发在MainActivity之前添加一个含有按钮页面跳转
- 用友nc65 uap审批流按钮开发
- JavaScript html js页面刷新之后下拉菜单选中值不变,在按钮提交后保存下拉列表中值不变
- os x应用点击关闭按钮退出程序的方法
- Cocoa and Cocoa Touch 一个是用于开发OS X的app 一个是用于开发iPhone的app
- iOS开发>学无止境 - 自定义导航按钮UIBarButtonItem
- 开发小技巧:超简单的“回到顶端”按钮实现
- Android开发 更改返回按钮的图标
- ios开发-新浪微博-09(下拉菜单封装一)
- iOS开发:动态添加按钮
- Android开发:自定义界面的Dialog,并且实现按钮的监听回调
- iOS开发-UI控件:自定义UITableViewCell 上的多个按钮点击事件处理
- VC6.0开发OCX按钮控件
- Windows SDK开发之按钮自绘