自定义NSButton实现右键菜单

最终实现效果.

左键为普通按钮,右键会弹出一个菜单

3208767-70ffaade12081774.png

实现方法

1, 重写NSButton

 -(void)rightMouseDown:(NSEvent *)theEvent{

//新建菜单
    NSMenu* newMenu = [[NSMenu allocWithZone:NSDefaultMallocZone()] initWithTitle:@"Name"];
    NSMenuItem* newItem = [[NSMenuItem alloc]init];

//设置菜单内容
    NSRect frameRect = NSMakeRect(0, 0, 200, 80);
    NSView *boundView = [[NSView alloc]initWithFrame:frameRect];

    NSRect frameTitle1 = NSMakeRect(2, 58, 30, 20);
    NSTextField *title1 = [[NSTextField alloc]initWithFrame:frameTitle1];
    title1.backgroundColor = [NSColor windowBackgroundColor];
    title1.stringValue = @"Title";
    title1.enabled = NO;
    title1.bordered = NO;
    NSRect frameText1 = NSMakeRect(32, 58, 160, 20);
    NSTextField *Text1 = [[NSTextField alloc]initWithFrame:frameText1];
   Text1.stringValue = self.title;

    NSRect frameTitle2 = NSMakeRect(2, 36, 35, 20);
    NSTextField *title2 = [[NSTextField alloc]initWithFrame:frameTitle2];
    title2.backgroundColor = [NSColor windowBackgroundColor];
    title2.stringValue = @"CMD";
    title2.enabled = NO;
    title2.bordered = NO;
    NSRect frameText2 = NSMakeRect(32, 36, 160, 20);
    NSTextField *Text2 = [[NSTextField alloc]initWithFrame:frameText2];
    Text2.stringValue = self.alternateTitle;

    NSRect frameCancel = NSMakeRect(30, 10, 60, 20);
    NSButton *cancle = [[NSButton alloc]initWithFrame:frameCancel];
//    [cancle setBordered:NO];
    [cancle setAction:@selector(cancleButton:)];
    [cancle setBezelStyle:NSBezelStyleRounded];
    cancle.title = @"取消";
    NSRect frameOK = NSMakeRect(100, 10, 60, 20);
    NSButton *OK = [[NSButton alloc]initWithFrame:frameOK];
//    [OK setBordered:NO];
  [OK setBezelStyle:NSBezelStyleRounded];
 OK.title = @"保存";
 [OK setAction:@selector(OKButton:)];
//    [OK setKeyEquivalent:@"/r"];
 [boundView addSubview:title1];
[boundView addSubview:Text1];
[boundView addSubview:title2];
[boundView addSubview:Text2];
[boundView addSubview:cancle];
[boundView addSubview:OK];

[MenuItemAry removeAllObjects];
[MenuItemAry addObjectsFromArray:@[newMenu,Text1,Text2]];
[newItem setView:boundView];

[newItem setEnabled:YES];
[newItem setTarget:self];
[newMenu addItem:newItem];

//设置菜单响应委托
 [NSMenu popUpContextMenu:newMenu withEvent:theEvent forView:self];
}

实现菜单中按键响应

 -(void)cancleButton:(id)cancle{
     NSMenu *aa = MenuItemAry[0];
    [aa cancelTracking];
}

-(void)OKButton:(id)ok{
    NSTextField *Title = MenuItemAry[1];
    NSTextField *Text = MenuItemAry[2];
    self.title = Title.stringValue;
    self.alternateTitle = Text.stringValue;
    NSLog(@"%@",self.alternateTitle);
    NSMenu *aa = MenuItemAry[0];
    [aa cancelTracking];
}

2, 拖入NSButton控件

3,选择Class为自定义类

屏幕快照 2017-01-24 上午9.31.44.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,593评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,554评论 0 17
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,259评论 4 61
  • 作家闫红自传随笔集。 我们眼前的世界,是活的。回头望望,身后的世界,却一寸寸死掉,烧成灰烬。故乡故人,过去的记忆大...
    草上霜阅读 251评论 0 0
  • 现在规定不能出现新增不良贷款,只有将存量不良贷款回收后才有腾挪空间。 减罚息回收“陈年老账” 在不良贷款增加的压力...
    临江李采忠阅读 755评论 0 0