单击菜单项时没有回调

Posted

技术标签:

【中文标题】单击菜单项时没有回调【英文标题】:No callback when clicking menu item 【发布时间】:2015-11-01 12:36:12 【问题描述】:

我正在尝试在我的 FinderSync 扩展中实现一个简单的上下文菜单。

我使用一些示例构建了以下内容,我的问题是当我单击菜单项时从未调用过回调。

源代码:

ContextMenuHelper.h

#import <Foundation/Foundation.h>
#include "FinderSync.h"

@interface ContextMenuHelper : NSObject

+ (NSMenu *)buildMenu;

@end

ContextMenuHelper.m

#import "ContextMenuHelper.h"

#define SharedContextMenuTarget  [ContextMenuTarget sharedInstance]

@interface ContextMenuTarget : NSObject
+ (ContextMenuTarget *) sharedInstance;
@end

@implementation ContextMenuTarget

- (void) callback              : (id)sender 
    NSLog(@"Called back!!!");


+ (ContextMenuTarget *) sharedInstance

    static ContextMenuTarget *sharedContextMenuTarget = nil;
    @synchronized(self)
    
        if (!sharedContextMenuTarget)
            sharedContextMenuTarget = [[ContextMenuTarget alloc] init];
        return sharedContextMenuTarget;
    


@end

@implementation ContextMenuHelper

+ (NSMenu *)buildMenu

    ContextMenuTarget *contextMenuTarget = SharedContextMenuTarget;

    NSMenu *mySubmenu = [[NSMenu alloc] initWithTitle:@""];

    NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"hello"
                                                     action:@selector(callback:)
                                              keyEquivalent:@""];
    [newMenu setTarget:contextMenuTarget];
    [mySubmenu addItem:newMenu];

    return mySubmenu;


@end

MyFinderSync.m

...
- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu 

    NSMenu *myContextMenu = [[NSMenu alloc] initWithTitle:@""];

    @try
    
        if(whichMenu != FIMenuKindContextualMenuForItems) 
            return myContextMenu;
        

        myContextMenu = [ContextMenuHelper buildMenu];
    
    @catch (NSException *ex)
    
    

    return myContextMenu;

...

【问题讨论】:

我发现了以下示例:github.com/liferay/liferay-nativity/blob/… 看起来比我尝试的要复杂得多。真的有必要吗? 【参考方案1】:

显然,只有当目标是FinderSync 的实例时,回调才会起作用。找不到任何文档来支持这一理论,但唯一解决问题的方法是将上下文菜单代码移动到 MyFinderSync.m

...
- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu 

    NSMenu *myContextMenu = [[NSMenu alloc] initWithTitle:@""];

    @try
    
        if(whichMenu != FIMenuKindContextualMenuForItems) 
            return myContextMenu;
        

        myContextMenu = [self buildMenu];
    
    @catch (NSException *ex)
    
    

    return myContextMenu;


- (NSMenu *)buildMenu

    NSMenu *mySubmenu = [[NSMenu alloc] initWithTitle:@""];

    NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"hello"
                                                     action:@selector(callback:)
                                              keyEquivalent:@""];
    [newMenu setTarget:self];
    [mySubmenu addItem:newMenu];

    return mySubmenu;


- (void) callback              : (id)sender 
    NSLog(@"Called back!!!");

...

【讨论】:

以上是关于单击菜单项时没有回调的主要内容,如果未能解决你的问题,请参考以下文章

单击其他菜单项时隐藏当前子菜单

单击另一个菜单项时如何切换菜单

右键单击任何菜单项时如何获取 MenuItem 名称?

单击侧菜单项时如何避免刷新页面

在java中单击菜单项时如何更改形状

单击菜单活动项时标题滚动到顶部