OS X开发NSMenu应用详解

简介: OS X开发NSMenu应用详解

一、引言


   NSMenu在Mac桌面软件开发中往往有3个方面的应用,作为程序的主菜单栏使用,作为视图邮件菜单使用和作为Dock菜单使用。


二、主应用菜单


   使用Xcode新建OX S应用时,可以选择使用Storyboard。Storyboard里面会自动创建一个菜单栏,你可以自行在菜单中进行增删改操作,菜单中的Item触发方法也可以直接与AppDelegate进行关联,实现自定义的菜单逻辑,如图:


image.png


三:Dock菜单


   当一款Mac桌面软件运行时,会在Dock栏上显示一个图标,当在此图标上点击右键时,会出现一个Dock菜单,自定义此Dock菜单也十分容易,直接在AppDelegate中重写如下方法即可:


-(NSMenu *)applicationDockMenu:(NSApplication *)sender{

   NSMenu * menu = [[NSMenu alloc]initWithTitle:@"Menu"];

   NSMenuItem * item1 = [[NSMenuItem alloc]initWithTitle:@"菜单1" action:@selector(click) keyEquivalent:@""];

   item1.target = self;

   NSMenuItem * item2 = [[NSMenuItem alloc]initWithTitle:@"菜单2" action:@selector(click) keyEquivalent:@""];

   item2.target = self;

   NSMenuItem * item3 = [[NSMenuItem alloc]initWithTitle:@"菜单3" action:@selector(click) keyEquivalent:@""];

   NSMenu * subMenu = [[NSMenu alloc]initWithTitle:@"subMenu"];

   NSMenuItem * item4 = [[NSMenuItem alloc]initWithTitle:@"菜单4" action:@selector(click) keyEquivalent:@""];

   item4.target = self;

   [subMenu addItem:item4];

   [menu addItem:item1];

   [menu addItem:item2];

   [menu addItem:item3];

   [menu setSubmenu:subMenu forItem:item3];

   return menu;

}

效果如下:


image.png


四、视图右键弹出菜单


   视图右键弹出菜单是基于NSView视图的,例如:


- (void)viewDidLoad {

   [super viewDidLoad];

   NSMenu * menu = [[NSMenu alloc]initWithTitle:@"Menu"];

   NSMenuItem * item1 = [[NSMenuItem alloc]initWithTitle:@"菜单1" action:@selector(click) keyEquivalent:@""];

   item1.target = self;

   NSMenuItem * item2 = [[NSMenuItem alloc]initWithTitle:@"菜单2" action:@selector(click) keyEquivalent:@""];

   item2.target = self;

   NSMenuItem * item3 = [[NSMenuItem alloc]initWithTitle:@"菜单3" action:@selector(click) keyEquivalent:@""];

   NSMenu * subMenu = [[NSMenu alloc]initWithTitle:@"subMenu"];

   NSMenuItem * item4 = [[NSMenuItem alloc]initWithTitle:@"菜单4" action:@selector(click) keyEquivalent:@""];

   item4.target = self;

   [subMenu addItem:item4];

   [menu addItem:item1];

   [menu addItem:item2];

   [menu addItem:item3];

   [menu setSubmenu:subMenu forItem:item3];

   [self.view setMenu:menu];

}

效果如下:

image.png



五、NSMenuItem详解


   NSMenuItem是菜单中的每一个菜单选项对象,其中常用属性方法如下:


//设置是否启用用户快捷键

+ (void)setUsesUserKeyEquivalents:(BOOL)flag;

//设置用户快捷键启用状态

+ (BOOL)usesUserKeyEquivalents;

//创建一个分割线

+ (NSMenuItem *)separatorItem;

//使用标题,快捷键和方法选择器来对Item进行初始化

- (instancetype)initWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode;

//其所在的菜单对象

@property (nullable, assign) NSMenu *menu;

//其是否有子菜单

@property (readonly) BOOL hasSubmenu;

//子菜单对象

@property (nullable, strong) NSMenu *submenu;

//如果此Item是某个子菜单中的,此属性获取与子菜单关联的父item

@property (nullable, readonly, assign) NSMenuItem *parentItem;

//Item标题

@property (copy) NSString *title;

//富文本标题

@property (nullable, copy) NSAttributedString *attributedTitle;

//是否是分隔线Item

@property (getter=isSeparatorItem, readonly) BOOL separatorItem;

//绑定的快捷键

@property (copy) NSString *keyEquivalent;

//快捷键类型

/*

typedef NS_OPTIONS(NSUInteger, NSEventModifierFlags) {

   NSEventModifierFlagCapsLock           = 1 << 16, // Caps lock键

   NSEventModifierFlagShift              = 1 << 17, // shift键

   NSEventModifierFlagControl            = 1 << 18, // control键

   NSEventModifierFlagOption             = 1 << 19, // option键

   NSEventModifierFlagCommand            = 1 << 20, // command键

   NSEventModifierFlagNumericPad         = 1 << 21, // 小键盘任意键

   NSEventModifierFlagHelp               = 1 << 22, // 帮助键

   NSEventModifierFlagFunction           = 1 << 23, // 任意功能按钮

};

*/

@property NSEventModifierFlags keyEquivalentModifierMask;

//Item图标

@property (nullable, strong) NSImage *image;

//Item状态

@property NSInteger state;

//开启状态下的图标

@property (null_resettable, strong) NSImage *onStateImage;

//关闭状态下的图标

@property (nullable, strong) NSImage *offStateImage;

//混合状态下的图标

@property (null_resettable, strong) NSImage *mixedStateImage;

//是否有效

@property (getter=isEnabled) BOOL enabled;

//是否前置

@property (getter=isAlternate) BOOL alternate;

//Item缩进级别

@property NSInteger indentationLevel;

//设置交互响应者

@property (nullable, weak) id target;

//设置交互相应方法

@property (nullable) SEL action;

//设置tag值

@property NSInteger tag;

//是否高亮

@property (getter=isHighlighted, readonly) BOOL highlighted;

//设置是否隐藏

@property (getter=isHidden) BOOL hidden;

//设置提示文本

@property (nullable, copy) NSString *toolTip;

六、NSMenu详解


//初始化方法

- (instancetype)initWithTitle:(NSString *)title;

//标题

@property (copy) NSString *title;

//在所在的交互点弹出菜单

+ (void)popUpContextMenu:(NSMenu*)menu withEvent:(NSEvent*)event forView:(NSView*)view;

+ (void)popUpContextMenu:(NSMenu*)menu withEvent:(NSEvent*)event forView:(NSView*)view withFont:(nullable NSFont*)font;

- (BOOL)popUpMenuPositioningItem:(nullable NSMenuItem *)item atLocation:(NSPoint)location inView:(nullable NSView *)view NS_AVAILABLE_MAC(10_6);

//设置菜单栏是否可见

+ (void)setMenuBarVisible:(BOOL)visible;

+ (BOOL)menuBarVisible;

//父菜单

@property (nullable, assign) NSMenu *supermenu;

//插入Item

- (void)insertItem:(NSMenuItem *)newItem atIndex:(NSInteger)index;

- (NSMenuItem *)insertItemWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode atIndex:(NSInteger)index;

//添加Item

- (void)addItem:(NSMenuItem *)newItem;

- (NSMenuItem *)addItemWithTitle:(NSString *)string action:(nullable SEL)selector keyEquivalent:(NSString *)charCode;

//删除某个位置的Item

- (void)removeItemAtIndex:(NSInteger)index;

//删除Item

- (void)removeItem:(NSMenuItem *)item;

//为某个Item设置子菜单

- (void)setSubmenu:(nullable NSMenu *)menu forItem:(NSMenuItem *)item;

//删除所有Item

- (void)removeAllItems;

//Item数组

@property (readonly, copy) NSArray<NSMenuItem *> *itemArray;

//获取Item个数

@property (readonly) NSInteger numberOfItems;

//获取某个位置的Item

- (nullable NSMenuItem *)itemAtIndex:(NSInteger)index;

//获取某个Item的位置

- (NSInteger)indexOfItem:(NSMenuItem *)item;

- (NSInteger)indexOfItemWithTitle:(NSString *)title;

- (NSInteger)indexOfItemWithTag:(NSInteger)tag;

- (NSInteger)indexOfItemWithSubmenu:(nullable NSMenu *)submenu;

- (NSInteger)indexOfItemWithTarget:(nullable id)target andAction:(nullable SEL)actionSelector;

//根据标题获取item

- (nullable NSMenuItem *)itemWithTitle:(NSString *)title;

//根据tag获取Item

- (nullable NSMenuItem *)itemWithTag:(NSInteger)tag;

//刷新菜单

- (void)update;

//获取菜单高度

@property (readonly) CGFloat menuBarHeight;

//取消菜单

- (void)cancelTracking;

- (void)cancelTrackingWithoutAnimation;

//获取高亮的Item

@property (nullable, readonly, strong) NSMenuItem *highlightedItem;

//最小宽度

@property CGFloat minimumWidth;

//尺寸

@property (readonly) NSSize size;

//字体

@property (null_resettable, strong) NSFont *font;

目录
相关文章
|
16天前
|
存储 Unix 程序员
面试题:Ctrl + C在不同操作系统下的应用
字节跳动面试题:Ctrl + C在不同操作系统下的应用
35 1
|
2月前
|
存储 弹性计算 安全
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(1)
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(1)
335 1
|
2月前
|
JavaScript 前端开发 开发者
深入理解ArkTS:Harmony OS 应用开发语言 TypeScript 的基础语法和关键特性
深入理解ArkTS:Harmony OS 应用开发语言 TypeScript 的基础语法和关键特性
60 0
|
2月前
|
存储 弹性计算 安全
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(2)
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(2)
664 1
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(2)
|
1天前
|
前端开发 Android开发 开发者
移动应用与系统:探索开发实践与操作系统融合
【4月更文挑战第23天】 在数字化时代的浪潮中,移动应用与系统的发展已成为技术创新和商业竞争的前沿阵地。本文将深入探讨移动应用的开发实践、移动操作系统的最新趋势以及二者之间的紧密融合。通过分析跨平台工具的效能、原生开发的优化策略以及操作系统的定制化功能,旨在为开发者提供一个全面而深入的视角,以适应不断变化的市场需求和技术挑战。
|
14天前
|
监控 测试技术 Android开发
移动应用与系统:开发与操作系统的深度解析
【4月更文挑战第11天】在这篇文章中,我们将深入探讨移动应用的开发过程,以及移动操作系统如何影响这些应用的性能和功能。我们将详细分析移动应用开发的关键步骤,包括需求分析、设计、编码、测试和维护。同时,我们也将探讨移动操作系统,如Android和iOS,如何为应用开发提供支持,并影响其性能。
|
16天前
|
Java 物联网 Android开发
移动应用与系统:探索开发与操作系统的融合
【4月更文挑战第8天】 在数字化时代的浪潮中,移动应用与系统的发展已成为技术创新的前沿阵地。本文将深入探讨移动应用开发的关键技术、移动操作系统的架构演变以及两者之间的相互作用。通过对当前流行的开发工具、编程语言和操作系统特性的分析,我们旨在为开发者提供一套全面的指导,以优化移动应用的性能和用户体验。同时,文章还将展望移动技术的未来趋势,特别是在人工智能和物联网领域的潜在应用。
|
27天前
|
机器学习/深度学习 搜索推荐 vr&ar
移动应用与系统的未来:从开发到操作系统的全面解析
本文将探讨移动应用和系统的未来,包括移动应用开发的趋势,移动操作系统的进展,以及这两者如何相互影响。我们将深入探讨如何通过理解这些变化,来优化移动应用的性能和用户体验。
10 2
|
2月前
|
弹性计算 运维 监控
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(3)
带你读《从基础到应用云上安全航行指南》——九大提升ECS实例操作系统安全性的技巧(3)
340 1
|
2月前
|
Unix Linux iOS开发
操作系统透视:从历史沿革到现代应用,剖析Linux与网站服务架构
操作系统透视:从历史沿革到现代应用,剖析Linux与网站服务架构
56 0