首页 > 其他分享 >Mac开发_NSPopUpButton

Mac开发_NSPopUpButton

时间:2022-11-15 15:12:56浏览次数:37  
标签:NSInteger title menu pop Mac 开发 NSPopUpButton 按钮 btn

1、基础创建

  • 代码
// 创建
NSPopUpButton *pop_btn = [[NSPopUpButton alloc] init];
// 位置尺寸
pop_btn.frame = CGRectMake(50, 150, 120, 50);
// 添加
[self.window.contentView addSubview:pop_btn];
// pullsDown 设置为 YES 只有向下的箭头
pop_btn.pullsDown = NO;
// 当交互事件发生时,是否禁用选项
pop_btn.autoenablesItems = YES;
// 弹出菜单的位置
pop_btn.preferredEdge = NSRectEdgeMaxX;

// 逐个添加项目
[pop_btn addItemWithTitle:@"城市"];
[pop_btn addItemWithTitle:@"上海"];
[pop_btn addItemWithTitle:@"广州"];
// 批量添加项目
[pop_btn addItemsWithTitles:@[@"深圳", @"河南", @"桂林"]];

// 添加点击事件
[pop_btn setTarget:self];
[pop_btn setAction:@selector(pop_Tap:)];

// 点击选中事件
- (void)pop_Tap:(NSPopUpButton *)pop_btn {
    pop_btn.title = pop_btn.selectedItem.title;
    
    NSLog(@"NSPopUpButton == %@", pop_btn.title);
}
  • 效果

2、方法说明

// 初始化方法 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;

3、自定义子项目

  • 3.1 创建

// 创建
NSPopUpButton *pop_btn = [[NSPopUpButton alloc] init];
// 位置尺寸
pop_btn.frame = CGRectMake(50, 200, 120, 50);
// 添加
[self.window.contentView addSubview:pop_btn];
// pullsDown 设置为 YES 只有向下的箭头
pop_btn.pullsDown = NO;
// 当交互事件发生时,是否禁用选项
pop_btn.autoenablesItems = YES;
// 弹出菜单的位置
pop_btn.preferredEdge = NSRectEdgeMaxX;

NSArray *pop_Items = @[@"广州", @"深圳", @"桂林", @"广州", @"深圳", @"桂林", @"广州", @"深圳", @"桂林", @"广州", @"深圳", @"桂林"];
for (NSInteger index = 0; index < pop_Items.count; index++) {
    GC_MenuItem *menu_Item = [[GC_MenuItem alloc ] init];
    menu_Item.title = pop_Items[index];
    [pop_btn.menu addItem:menu_Item];
    menu_Item.target = self;
    menu_Item.action = @selector(pop_Tap:);
}

// 点击选中事件
- (void)pop_Tap:(NSPopUpButton *)pop_btn {
    pop_btn.title = pop_btn.selectedItem.title;
    
    NSLog(@"NSPopUpButton == %@", pop_btn.title);
}
  • 3.2 自定义类

    • GC_MenuItem.h
    #import <Cocoa/Cocoa.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface GC_MenuItem : NSMenuItem
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    • GC_MenuItem.m
    #import "GC_MenuItem.h"
    
    @interface GC_MenuView : NSView
    
    @end
    
    @implementation GC_MenuView
    
    - (void)touchesBeganWithEvent:(NSEvent *)event {
        [super touchesBeganWithEvent:event];
        
        NSLog(@"1234567890");
        
    }
    
    @end
    
    @implementation GC_MenuItem
    
    - (void)setTitle:(NSString *)title {
        [super setTitle:title];
        
        GC_MenuView *menu_view = [[GC_MenuView alloc] init];
        menu_view.frame = NSMakeRect(0, 0, 200, 30);
        [self setView:menu_view];
        
        menu_view.wantsLayer = YES;
        if ([title isEqualToString:@"深圳"]) {
            menu_view.layer.backgroundColor = [NSColor redColor].CGColor;
        }
        else if ([title isEqualToString:@"广州"]) {
            menu_view.layer.backgroundColor = [NSColor lightGrayColor].CGColor;
        }
        else {
            menu_view.layer.backgroundColor = [NSColor blueColor].CGColor;
        }
    }
    
    @end
    
  • 3.3 效果

标签:NSInteger,title,menu,pop,Mac,开发,NSPopUpButton,按钮,btn
From: https://www.cnblogs.com/CH520/p/15944900.html

相关文章

  • 软件开发架构
    软件开发架构目录软件开发架构c/s架构b/s架构架构总结网络编程前戏OSI七层协议简介每层运行常见物理设备每层运行常见的协议OSI七层协议功能TCP/IP四层协议模型五:OSI......
  • 软件开发架构、架构总结、网络编程前戏、OSI七层协议简介、OSI七层协议之物理连接层、
    软件开发架构规定了程序的请求逻辑、功能分块1.C/S架构 Client:客户端 Server:服务端 """ 我们使用计算机下载下来的一个个app本质是各大互联网公司的客户端软件......
  • 软件开发架构
    软件开发架构1.C/S架构Client:客户端Server:服务端"""我们使用计算机下载下来的一个个app本质是各大互联网公司的客户端软件通过客户端软件,我们就可以体验到各个互联网......
  • 直播软件app开发,js 表单验证,登录注册
     直播软件app开发,js表单验证,登录注册<!DOCTYPEhtml><html> <head>  <metacharset="UTF-8">  <metahttp-equiv="X-UA-Compatible"content="IE=edge">  ......
  • AMD处理器(桌面级)可以安装黑苹果macOS吗?
    可以。使用​​Clover​​​或​​OpenCore​​都可以。目前​​AMD​​处理器搭配内核补丁使用​​OpenCore​​安装黑苹果比使用​​Clover​​更容易。但是,截止2021年5月......
  • MACOS屏幕录制
    1文章来自:吕大千字节跳动飞书 2022-07-10 WWDC22 iOS;如有侵权请及时反馈联系;非商用仅记录;支持小专栏正版https://xiaozhuanlan.com/topic/0458326917 目前ScreenC......
  • Affinity Photo for Mac(矢量图处理软件)v2.0.0中文版mac/win
    AffinityDesigner是一款专业的矢量图处理软件,可以帮助用户对矢量图进行编辑处理,软件为用户提供许多专业的矢量图功能、工具和面板,用户可以在软件中轻松找到自己需要的功能......
  • 为什么工业软件开发一般用的都是QT?
    这个说法不大准确吧,CAD、MATLAB、EDA、PRO-E、Proteus、POWERPCB是用的QT么?SAP、EBS(ORACLE)、LabView是用的QT么?西门子的工业控制类的软件(比如SIMATICSCADA)用的QT么?这些......
  • Qt音视频开发02-海康sdk解码(支持句柄/回调/gpu模式/支持win/linux)
    一、前言为何还要选用使用海康sdk,之前不是ffmpeg已经牛皮吹上天了吗?这个问题问得好,那是因为无论ffmpeg也好还是vlc/mpv之类的,都是实现的播放相关,不同的监控硬件厂家对应设......
  • Microsoft Office LTSC 2021 for Mac永久版 v16.68 beta版mac/win
    微软公司发布了office 2021Standard(预览版),以在组织中运行macOS的设备上进行安装和测试。office2021forMac商业预览版包括Word,Excel,PowerPoint,Outlook,OneDrive,最新版本......