How to remove th COPY UIMenuItem in UIMenuController

孤人 提交于 2019-12-21 06:49:03

问题


I am developing an iPhone application and worked with UIWebViews. I planned to customize the UIMenuItems thats popped up when i highlight strings in UiWebView (Copy, Select All, Cut, etc.). I tried removing those menuItems but i cannot remove the COPY menu item.

I need your help guys.

Thanks, ZaldzBugz


回答1:


You're going to have to do some hijacking. I don't even know if it is possible. It looks like it probably responds to a gesture event called UILongPressGestureRecognizer.

You need to find out what view has the gesture added to it. something like this.

NSArray *gestures = myWebView.gestureRecognizers;
for(int i=0;i<[gestures count];i++){
    UIGestureRecognizer g = [gestures objectAtIndex:i];
    if([g isKindOfClass:UILongPressGestureRecognizer.Class]){
       [myWebView removeGestureRecognizer:g];
       break;
    }
}

but I don't think the gesture is on the UIWebview directly. It is on one of its subviews.

So it might be on this subview

UIScrollView *sview = [[mywebview subviews] objectAtIndex:0];

If not on that one then it is probably on the view that is on top the most, which is what I suspect. It is an undocumented View type, but if you treat it as a UIView for the methods we are calling, it is presumably safe. Also, because you are treating it as a UIView, it isn't against the rules as far as I understand.

UIScrollView *sview = [[mywebview subviews] objectAtIndex:0];
UIView *realWebView = [[sview subviews] objectAtIndex:10];

The reason I didn't write the snippet (which is untested) to try to remove the gesture from all of the views is because it is important you know which view you remove it from.

After you remove it, you need to write your own UILongPressGestureRecognizer and then add the new one to the respective view (the view it was removed from).

You have lost all your functionality of copy select etc... but now you can implement your own.

If you want some of the old functionality, I think I know how you can get it back, but I can't promise this is allowed or safe.

Before you remove the gesture you need to override a method to UIView. You should be able to do it in your current working class file (at the top of the file)

@implementation UIView (extended)
     -(BOOL) respondsToSelector:(SEL)aSelector {
          printf("SELECTOR: %s\n", 
            [NSStringFromSelector(aSelector) UTF8String]);

          return [super respondsToSelector:aSelector];
     }
@end

I can't promise anything, but this should log the name of EVERY method that is called in your view.

Run your app and execute the copy,paste,select,select all methods. Your log should show what methods get called when you press the buttons. It may show several methods when you push a button, you probably just want the first.

Now when you create a menuItem you know what selector to assign to it.

This is all just a guess. Hope it helps.




回答2:


I used the technique in this answer to a similar question and it works for ridding of the Copy menu item. That is, override UIWebView instead of UITextView in their example. Also wrote a canPerformAction:withSender: in the view controller that returns YES for my custom selectors and NO for the copy: selector, then returns the value of the super implementation if the selector is none of those. Remember to set the custom UIWebView subclass name in your xib.



来源:https://stackoverflow.com/questions/3262530/how-to-remove-th-copy-uimenuitem-in-uimenucontroller

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