单击菜单项时无回调 [英] No callback when clicking menu item
本文介绍了单击菜单项时无回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在FinderSync扩展中实现一个简单的上下文菜单.
I'm trying to implement a simple context menu in my FinderSync extension.
我使用一些示例构建了以下内容,但我的问题是,单击菜单项时从未调用过回调.
I built the following using some examples, and my problem is that the callback is never called when I click the menu item.
源代码:
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;
}
...
推荐答案
显然,仅当目标是FinderSync
的实例时,回调才有效.找不到支持该理论的任何文档,但是解决该问题的唯一方法是将上下文菜单代码移至 MyFinderSync.m :
Apparently, callbacks will only work if the target is the instance of 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:
...
- (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!!!");
}
...
这篇关于单击菜单项时无回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文