Remove copy and define from UIMenuController

蹲街弑〆低调 提交于 2019-12-11 19:59:34

问题


I have UIWebView for displaying some articles. I need to select some text from UIWebView and use bookmark. So i'm using selection = [wbCont stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]; But when i longpress the UIMenuItem displays with copy,define. I read some doc and used canPerformAction: copy NO. But still it displaying.

- (void)viewDidLoad
{
[wbCont loadHTMLString:webString baseURL:nil];
    [self.view addSubview:wbCont];
 NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
    if (!items) items = [[NSMutableArray alloc] init];

    UIMenuItem *menuItem;
    menuItem = [[UIMenuItem alloc] initWithTitle:@"BookMark" action:@selector(book:)];
    [items addObject:menuItem];
    [menuItem release];
    menuItem = [[UIMenuItem alloc] initWithTitle:@"Note" action:@selector(note:)];
    [items addObject:menuItem];
    [menuItem release];

   [[UIMenuController sharedMenuController] setMenuItems:items];


    [items release];
}



- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{


 if (action == @selector(copy:))
 {

 return NO;

 }
    if (action == @selector(book:))
    {
        return YES;
    }
    else if (action == @selector(note:))
    {
        return YES;
    }

    return [super canPerformAction:action withSender:sender];


}

回答1:


You have to subclass UIWebView. (Create a new Objective-C class and select subclass of UIWebView).

Within your subclass write the method:

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender{
 if (action == @selector(copy:))
 {

 return NO;

 }
    return [super canPerformAction:action withSender:sender];
}

You don't need to set you own custom selectors there if you are adding them inside your contoller (as I guess that's what you where doing).

More details can be found here: How do you REALLY remove Copy from UIMenuController



来源:https://stackoverflow.com/questions/19287903/remove-copy-and-define-from-uimenucontroller

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