Mac开发,NSMenu、NSMenuItem的简单使用
2017-12-21 17:50
561 查看
在想要显示菜单的视图文件中添加如下代码:
-(void)rightMouseDown:(NSEvent *)event{ //创建Menu NSMenu *theMenu = [[NSMenualloc] initWithTitle:@"Contextual Menu"]; //自定义的NSMenuItem NSMenuItem *item3 = [[NSMenuItemalloc]init]; self.customView.wantsLayer =YES; self.customView.layer.backgroundColor = [NSColorredColor].CGColor; item3.title = @"Item 3"; item3.view = self.customView; item3.target = self; item3.action = @selector(beep:); [theMenu insertItemWithTitle:@"Item 1"action:@selector(beep:)keyEquivalent:@""atIndex:0]; [theMenu insertItemWithTitle:@"Item 2"action:@selector(beep:)keyEquivalent:@""atIndex:1]; [theMenu insertItem:item3 atIndex:2]; [NSMenu popUpContextMenu:theMenuwithEvent:event forView:self.view];}-(void)beep:(NSMenuItem *)menuItem{ NSLog(@"_____%@", menuItem);}
- (void)setRepresentedObject:(id)representedObject { [super setRepresentedObject:representedObject];
// Update the view, if already loaded.}
-(void)rightMouseDown:(NSEvent *)event{ //创建Menu NSMenu *theMenu = [[NSMenualloc] initWithTitle:@"Contextual Menu"]; //自定义的NSMenuItem NSMenuItem *item3 = [[NSMenuItemalloc]init]; self.customView.wantsLayer =YES; self.customView.layer.backgroundColor = [NSColorredColor].CGColor; item3.title = @"Item 3"; item3.view = self.customView; item3.target = self; item3.action = @selector(beep:); [theMenu insertItemWithTitle:@"Item 1"action:@selector(beep:)keyEquivalent:@""atIndex:0]; [theMenu insertItemWithTitle:@"Item 2"action:@selector(beep:)keyEquivalent:@""atIndex:1]; [theMenu insertItem:item3 atIndex:2]; [NSMenu popUpContextMenu:theMenuwithEvent:event forView:self.view];}-(void)beep:(NSMenuItem *)menuItem{ NSLog(@"_____%@", menuItem);}
- (void)setRepresentedObject:(id)representedObject { [super setRepresentedObject:representedObject];
// Update the view, if already loaded.}
相关文章推荐
- Mac开发 - 使用CCMenu和CCMenuItem添加菜单、右击菜单、Dock菜单
- Mac开发学习笔记之NSTextField使用
- [cocoa]Mac开发知识:NSButton使用及自定义/Safari调用/NSTextField
- 使用omniORBpy开发简单CORBA程序
- 使用C#开发一个简单的P2P应用
- [webservices开发]使用XFire开发一个简单的web服务
- 使用C#开发一个简单的P2P应用
- 通过一个简单的SWING日历BEAN开发学习Calendar类的使用(1)Calendar概述
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- 使用C#开发一个简单的P2P应用
- 如何使用DOTMSN开发简单的MSN机器人示例(附示例源码)
- 一个简单的使用subversion开发的过程
- 使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- [webservices开发]使用XFire开发一个简单的web服务
- 使用C#开发一个简单的P2P应用
- 简单发布使用NetAdvantage开发的项目
- 使用C#开发一个简单的P2P应用
- 如何使用DOTMSN开发简单的MSN机器人示例(附示例源码)
- 使用C#开发一个简单的P2P应用