前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OS X开发:下拉菜单按钮NSPopUpButton应用

OS X开发:下拉菜单按钮NSPopUpButton应用

作者头像
珲少
发布2018-08-15 11:36:02
2.3K0
发布2018-08-15 11:36:02
举报
文章被收录于专栏:一“技”之长一“技”之长

OS X开发:下拉菜单按钮NSPopUpButton应用

    NSPopUpButton是一个下拉按钮,当用户点击时,其会弹出一个下拉选择菜单。一个简单的示例如下:

代码语言:javascript
复制
- (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类中属性和方法解析如下:

代码语言:javascript
复制
//初始化方法 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;
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/07/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OS X开发:下拉菜单按钮NSPopUpButton应用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档