检测单击上下文菜单osx

时间:2015-02-25 07:18:52

标签: objective-c xcode macos nsmenu nsmenuitem

每次扩展NSMenu时,如何确保调用方法。我已经尝试从故事板连接动作,但是当我单击菜单项而不是菜单本身时,动作似乎只会被触发。

[item setAction: @selector(play:)]

我想运行一个方法,例如帮助菜单扩展,更新该菜单的启用和禁用内容,因为它应该是不同的登录和未登录的用户..

更新

我在

中的括号中添加了NSMenuDelegate协议
 @interface ClientAppDelegate : NSObject<NSApplicationDelegate,NSMenuDelegate>

添加菜单项有效,但菜单似乎不会影响委托方法。

// Create the application on the UI thread.
- (void)createApplication:(id)object {
  NSApplication* application = [NSApplication sharedApplication];
  [NSBundle loadNibNamed:@"MainMenu" owner:NSApp];

  // Set the delegate for application events.
  [application setDelegate:self];

  // Add the Tests menu.
  NSMenu* menubar = [application mainMenu];

  [menubar setDelegate:self];
    // TIDAL Create Controlls
    NSMenuItem *controlsItem = [[[NSMenuItem alloc] initWithTitle:@"Controls"
                                                           action:nil
                                                    keyEquivalent:@""] autorelease];
    NSMenu *controlsMenu = [[[NSMenu alloc] initWithTitle:@"Controls"] autorelease];
    AddMenuItem(controlsMenu, @"Pl",      ID_L_PL);

    [controlsItem setSubmenu:controlsMenu];
    [menubar addItem:controlsItem];


......


-(void) menuWillOpen:(NSMenu *)menu{
  wprintf(L"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");
}

-(void) menuNeedsUpdate:(NSMenu *)menu{
  wprintf(L"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");

}

UPDATE2:

所以我重新开始,想象问题与引用有关,以及我在Objective-c ++中开发的事实。但是,我无法让它在最小的Obj-C示例中工作,下面是我的代码。唯一可行的回调是处理项目点击的回调。可能由于我正在使用这个问题而存在问题:

  _menubar = [application mainMenu];

  [_menubar setDelegate:self];

要获取我的菜单栏并设置我的菜单委托?

//
//  AppDelegate.m
//  menuTest
//
//  Created by David Karlsson on 27/02/15.
//  Copyright (c) 2015 David Karlsson. All rights reserved.
//

#import "AppDelegate.h"

void AddMenuItem(NSMenu *menu, NSString* label, int idval) {
  NSMenuItem* item = [menu addItemWithTitle:label
                                     action:@selector(menuItemSelected:)
                              keyEquivalent:@""];
  [item setTag:idval];
}


@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong) IBOutlet NSMenu * menubar;
@end

@implementation AppDelegate


- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
  NSLog(@"Launched menu test");
  // Insert code here to initialize your application
  NSApplication* application = [NSApplication sharedApplication];
  //[NSBundle loadNibNamed:@"MainMenu" owner:NSApp];

  // Set the delegate for application events.
  [application setDelegate:self];

  // Add the Tests menu.
  _menubar = [application mainMenu];

  [_menubar setDelegate:self];

  NSMenuItem *controlsItem = [[NSMenuItem alloc] initWithTitle:@"Controls"
                                                        action:nil
                                                 keyEquivalent:@""];
  NSMenu *controlsMenu = [[NSMenu alloc] initWithTitle:@"Controls"];
  AddMenuItem(controlsMenu, @"1",      123);
  AddMenuItem(controlsMenu, @"2",    124);
  AddMenuItem(controlsMenu, @"3",  154);

  [controlsItem setSubmenu:controlsMenu];
  [_menubar addItem:controlsItem];


}

-(void) menuWillOpen:(NSMenu *)menu{
  NSLog(@"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");
}

-(void) menuNeedsUpdate:(NSMenu *)menu{
  NSLog(@"ITEM CLICK CAN I UPDATE MENU VISIBILITY HERE?");

}




- (void)applicationWillTerminate:(NSNotification *)aNotification {
  // Insert code here to tear down your application
  NSLog(@"Teardown menu test");
}

- (IBAction)menuItemSelected:(id)sender {
  // Retrieve the active RootWindow.
  NSWindow* key_window = [[NSApplication sharedApplication] keyWindow];
  if (!key_window){
    return;
  }



  NSLog(@"CLICK");
}


@end

2 个答案:

答案 0 :(得分:1)

设置菜单的委托,然后使用此对象从menuNeedsUpdate:协议实现NSMenuDelegate。此方法在菜单显示之前调用,并且专门用于在有问题的菜单到达屏幕之前对其进行更改。

答案 1 :(得分:1)

您希望实现NSMenu Delegate协议的某些部分,并且您可能需要一个在菜单跟踪期间运行的模式下运行的NSTimer,以便在菜单可见时进行实时更新。