FinderSync context menu calls action in wrong class
I have a very strange problem here: I am writing a FinderSync extension, more specifically, adding items to the context menu. Now if I pack everything into one class, it works fine:
1) I have a main extension class called FinderSync
2) In this class, I am implementing
- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu
3) In this method, I write (by the way):
NSMenuItem *myItem = [[NSMenuItem alloc]
initWithTitle:@"myTitle"
action:@selector(myAction:)
keyEquivalent:@""];
4) There is a method in FinderSync class
- (IBAction)myAction:(id)sender;
5) This method is called expected when the user clicks on a menu item.
Now: I am trying to pass context menu functions to another class called ContextMenuHandler. The situation now:
1) I have a main extension class called FinderSync and another class ContextMenuHandler. FinderSync creates a ContextMenuHandler instance and stores a reference (_contextMenuHandler) to it.
2) Both classes implement
- (NSMenu *)menuForMenuKind:(FIMenuKind)whichMenu
3) FinderSync implementation of the ForMenuKind does nothing but call
return [_contextMenuHandler menuForMenuKind:whichMenu];
4) ContextMenuHandler creates NSMenuItem. (Exactly the same code). I even tried to add:
[myItem setTarget:self];
[myItem setAction:@selector(myAction:)];
Both classes implement myAction.
5) Expected: myAction from ContextMenuHandler is called after clicking on the menu item Observed: myAction FinderSync is called ...
I also restart finder after updates and the breakpoint in ContextMenuHandler hits correctly, so it doesn't seem to be a problem with the running version of FinderSync.
Does anyone have an explanation (or fix) for this strange behavior?
source to share
The docs FIFinderSyncProtocol
provide useful context:
menu(for menu: FIMenuKind)
The main extension object provides a method for each assigned menu item.
Indicates that the "main extension object" provides an action method, as you notice.
Special properties of the menu item are used: name, action, image and enabled.
Since 10.11: tag, state and indentationLevel also work and submenus are allowed.
Notably, this list of supported NSMenuItem
properties does not appear target
. The specified target for the action cannot be set, so Apple simply calls the action in a FinderSync subclass.
I faced a similar problem with a property representedObject
. Not saved by the time we get the menu item sender
.
source to share