简体   繁体   English

单击菜单项时无回调

[英]No callback when clicking menu item

I'm trying to implement a simple context menu in my FinderSync extension. 我正在尝试在FinderSync扩展中实现一个简单的上下文菜单。

I built the following using some examples, and my problem is that the callback is never called when I click the menu item. 我使用一些示例构建了以下示例,但我的问题是,单击菜单项时从未调用过回调。

Source code: 源代码:

ContextMenuHelper.h ContextMenuHelper.h

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

@interface ContextMenuHelper : NSObject

+ (NSMenu *)buildMenu;

@end

ContextMenuHelper.m 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 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;
}
...

Apparently, callbacks will only work if the target is the instance of FinderSync . 显然,仅当目标是FinderSync实例时,回调才有效。 Could not find any documentation to support this theory, but the only thing that fixed the problem was moving the context menu code into MyFinderSync.m : 找不到支持该理论的任何文档,但是解决该问题的唯一方法是将上下文菜单代码移至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!!!");
}
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Firebreath NPAPI插件中将NSMenu连接到NSStatusItem,以便在单击StatusBar项目时出现菜单? - How to connect NSMenu to NSStatusItem in a Firebreath NPAPI plugin so the menu appears when clicking the StatusBar Item? 单击横幅按钮项时,应用程序崩溃 - App crashes when clicking banner button item 单击外部视图时,使菜单栏视图不隐藏 - Make menu bar view not hide when clicking outside view 单击UITextField iOS时如何显示弹出菜单列表? (Kxmenu) - How show popup menu list when clicking on UITextField iOS? (Kxmenu) 在通知回调中设置时,标签栏上的徽章不显示 - Badge not appearing on tab bar item when set in notification callback 单击菜单项时获取菜单项位置Cocos2D(将它们传递给函数) - Getting menu item location when menu item is clicked Cocos2D (passing them into a function) 将IBAction连接到Objective-C中的菜单项时出错 - Error when connecting an IBAction to menu item in objective-c 有没有办法控制打开菜单时最初突出显示的NSMenuItem项? - Is there a way to control the NSMenuItem item that is initially highlighted when opening a menu? 从菜单中选择项目时,退出了TrackingArea-&gt;鼠标中的NSPopUpButton - NSPopUpButton in a TrackingArea -> mouse exited when selecting an item from the menu 单击选项卡栏项时如何将UIScrollView重置为零页/索引? - How do I reset UIScrollView to Zeroth Page/Index when Clicking on a Tab Bar Item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM