问题
I'm building my own custom Edit Menu (UIMenuController) and am using the typical
-(BOOL)canPerformAction:(SEL)action withSender(id)sender
method to conditionally enable / disable system defaults. The typical edit methods are well documented (copy:, cut:, etc.), but I can't find anything about what method is called by the "Define" menu option to pull up the new word dictionary in iOS 5. Maybe it's hiding in plain sight, but I've spent hours looking for it, so I'd appreciate any help. Specifically, I need:
if (action == @selector(defineWord:)) ......
but give me what really goes in the place of "defineWord:"
ps - I wouldn't mind knowing what class the method belongs to, just out of curiosity (copy: belongs to UIResponderStandardEditActions, for example)
回答1:
By implementing this:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
NSLog(@"%@", NSStringFromSelector(action));
}
I was able to see that the selector is "_define:".
回答2:
The define selector (_define:) is actually private and your app will be rejected if you use it. What I had to do to get only the Define menu item to show up in a UITextView was this:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(cut:) ||
action == @selector(copy:) ||
action == @selector(select:) ||
action == @selector(selectAll:) ||
action == @selector(paste:) ||
action == @selector(delete:))
{
return NO;
}
else return [super canPerformAction:action withSender:sender];
}
回答3:
In iOS 7.1 I see these actions occurring when overriding the canPerformAction:withSender:
method on a subclass of UIWebView
:
cut:
copy:
select:
selectAll:
paste:
delete:
_promptForReplace:
_showTextStyleOptions:
_define:
_addShortcut:
_accessibilitySpeak:
_accessibilitySpeakLanguageSelection:
_accessibilityPauseSpeaking:
makeTextWritingDirectionRightToLeft:
makeTextWritingDirectionLeftToRight:
Presumably the ones prefixed with an underscore are "private API" whose use subjects your app to rejection by the App Store. But I cannot really find any documentation on that one way or the other.
The ones without an underscore are defined as part of the UIResponderStandardEditActions informal protocol.
回答4:
Easy way to do it without using privateAPI, return YES only for wanted Action
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(MySelector:)
{
return [super canPerformAction:action withSender:sender]
}
else
return NO;
}
Enjoy ;)
来源:https://stackoverflow.com/questions/8021356/selector-for-define-edit-menu-item-in-ios-5