How to find the responder that is returning YES to canPerformAction:withSender:

…衆ロ難τιáo~ 提交于 2019-12-08 02:01:45

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!