[英]Detect click on context menu osx
How can I make sure a method gets called each time I expand a NSMenu. 如何确保每次我扩展NSMenu时都会调用一个方法。 I have tried connecting the action from storyboard but the action only seem to get fired when i click menu items, not the menu itself.
我尝试从情节提要中连接动作,但是仅在单击菜单项时才触发动作,而不是菜单本身。
[item setAction: @selector(play:)]
I would like to run a method when for instance the help menu gets expanded, to update the enabled and disabled content of that menu, since it is supposed to be different for logged in and not logged in users.. 我想在例如扩展帮助菜单时运行一种方法,以更新该菜单的启用和禁用内容,因为对于登录用户和未登录用户来说,该方法应该有所不同。
Update: 更新:
I added the NSMenuDelegate protocol in brackets in my 我在我的括号中添加了NSMenuDelegate协议
@interface ClientAppDelegate : NSObject<NSApplicationDelegate,NSMenuDelegate>
Adding menu items work but the menu does not seem to affect the delegate methods. 添加菜单项是可行的,但是菜单似乎并不影响委托方法。
// 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: 更新2:
So I started over, imaginging that the problem had something to do with references and the fact that i was developing in objective-c++. 因此,我重新开始,想象该问题与参考以及我正在使用Objective-C ++开发的事实有关。 However i can not get it to work in just a minimal Obj-C example either, below is my code.
但是我也无法在一个最小的Obj-C示例中使它工作,以下是我的代码。 The only callback that seems to work is the one that handles item clicks.
似乎唯一起作用的回调是处理项目单击的回调。 May the problem be present due to the fact that I am using :
可能由于我使用的事实而出现问题:
_menubar = [application mainMenu];
[_menubar setDelegate:self];
To get my menubar and setup my menu delegate? 要获取我的菜单栏并设置我的菜单代表?
//
// 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
Set the menu's delegate, then use this object to implement menuNeedsUpdate:
from the NSMenuDelegate
protocol. 设置菜单的委托,然后使用该对象从
NSMenuDelegate
协议实现menuNeedsUpdate:
NSMenuDelegate
This method is called just before a menu is shown and is specifically provided to allow you to make changes to the menu in question before it arrives on screen. 此方法在显示菜单之前被调用,并且专门提供该方法,以使您可以在菜单出现在屏幕上之前对其进行更改。
您要实现NSMenu Delegate协议的某些部分,并且可能需要在菜单跟踪过程中工作的模式下运行的NSTimer,以便在菜单可见时进行实时更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.