自定义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阅读 125,120评论 16 537
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 2,809评论 0 4
  • 作家闫红自传随笔集。 我们眼前的世界,是活的。回头望望,身后的世界,却一寸寸死掉,烧成灰烬。故乡故人,过去的记忆大...
    草上霜阅读 35评论 0 0
  • 现在规定不能出现新增不良贷款,只有将存量不良贷款回收后才有腾挪空间。 减罚息回收“陈年老账” 在不良贷款增加的压力...
    临江李采忠阅读 126评论 0 0
  • 任务三:教师任务 简介:教师即管理员,是组织课程并对系统运行进行控制的人,是由超级用户增加的,教师可以在课程进行过...
    明新ID阅读 27评论 0 0