我已经根据 Apple 提供的示例实现了 Finder Sync 扩展。
单击新创建的上下文菜单项后,将sampleAction
执行相应的操作。不幸的是,传递给该方法的发送者与创建的实例不匹配menuForMenuKind
。
编码
- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu
{
NSMenuItem* menuItem = [[NSMenuItem alloc] initWithTitle:@"testTitle" action:@selector(sampleAction:) keyEquivalent:@""];
menuItem.tag = 1;
// print menu attributes
NSLog(@"------------------original menu-----------------");
NSLog(@"menu address: %p", menuItem);
NSLog(@"menu tag: %ld", menuItem.tag);
NSLog(@"menu title: '%@'", menuItem.title);
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
[menu addItem:menuItem];
return menu;
}
- (IBAction)sampleAction:(id)sender
{
if( [sender isKindOfClass:[NSMenuItem class]])
{
NSMenuItem* menuItem = sender;
NSLog(@"------------------menu passed to action-----------------");
NSLog(@"menu address: %p", menuItem);
NSLog(@"menu tag: %ld", menuItem.tag);
NSLog(@"menu title: '%@'", menuItem.title);
}
}
在我单击上下文菜单中的元素后导致以下输出
2014-12-07 19:55:36.923 FinderSync Extension[1265:62630] ------------------original menu----------------
2014-12-07 19:55:36.923 FinderSync Extension[1265:62630] menu address: 0x6080000abbe0
2014-12-07 19:55:36.924 FinderSync Extension[1265:62630] menu tag: 1
2014-12-07 19:55:36.924 FinderSync Extension[1265:62630] menu title: 'testTitle'
2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] ------------------menu passed to action----------------
2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu address: 0x6080000aba00
2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu tag: 0
2014-12-07 19:55:40.328 FinderSync Extension[1265:62630] menu title: ''
.
是否有机会将发件人链接到单击的菜单项?