问题
I need to disable the Define menu item from the edit menu on a UIWebView. This is supposed to be done by implementing canPerformAction:withSender: and returning NO for the items to disable. Even though these are private items it seems like I should be able to return YES for the items I want to keep and NO for everything else (as in this question).
However this is not working. The documentation says that
If no responder in the responder chain returns YES, the menu command is disabled. Note that if your class returns NO for a command, another responder further up the responder chain may still return YES, enabling the command.
It seems that this must be the reason this isn't working. How do I find which responder is returning YES?
回答1:
In the end, I figured this out with this function which recursively goes through the subviews and logs whether they are first responder.
- (void) logResponderInfo: (UIView *)view
{
NSLog(@"%@ %@", NSStringFromClass(view.class), view.isFirstResponder ? @"yes" : @"no");
for (UIView *sub in view.subviews) {
[self logResponderInfo:sub];
}
}
Which I called from my canPerformAction:withSender: function
[self logResponderInfo:self.webView];
This wrote out to the logs
2013-11-18 11:35:56.100 Testing[44593:a0b] CDVCordovaView no
2013-11-18 11:35:56.100 Testing[44593:a0b] _UIWebViewScrollView no
2013-11-18 11:35:56.101 Testing[44593:a0b] UIWebBrowserView yes
2013-11-18 11:35:56.101 Testing[44593:a0b] UITextSelectionView no
2013-11-18 11:35:56.102 Testing[44593:a0b] UIView no
2013-11-18 11:35:56.102 Testing[44593:a0b] UIImageView no
2013-11-18 11:35:56.103 Testing[44593:a0b] UIImageView no
2013-11-18 11:35:56.103 Testing[44593:a0b] UIActivityIndicatorView no
2013-11-18 11:35:56.104 Testing[44593:a0b] UIImageView no
which told me that the first responder was in fact UIWebBrowserView.
来源:https://stackoverflow.com/questions/19280119/how-to-find-the-responder-that-is-returning-yes-to-canperformactionwithsender