4

I create a menu in UITableViewCell, this UIMenuController just has two items. but when i runing it, this menu displayed many items, seems ios default menu item, be shown as the screenshot:

enter image description here

How can i remove those items and just display my defined item? thx.

here is my code:

- (id)initWithComment:(DSComment *)comment
{
    self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"comment"];

    UILabel *contentLabel=[[UILabel alloc] initWithFrame:CGRectMake(10, 45, 300, 0)];
    contentLabel.text=comment.message;

    [self.contentView addSubview:contentLabel];
    return self;
}


- (BOOL) canBecomeFirstResponder {
    return YES;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self becomeFirstResponder];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    UIMenuItem *like = [[UIMenuItem alloc] initWithTitle:@"Like" action:@selector(like:)];
    UIMenuItem *reply = [[UIMenuItem alloc] initWithTitle:@"Replay" action:@selector(reply:)];

    [menu setMenuItems:[NSArray arrayWithObjects:like, reply, nil]];

    [menu setTargetRect:CGRectMake(0, 0, 0.0f, 0.0f) inView:self];
    [menu setMenuVisible:YES animated:YES];
}
Perchouli
  • 171
  • 1
  • 7
  • Dupe of http://stackoverflow.com/questions/10505755/removing-default-cut-copy-paste-from-uimenucontroller-in-a-tableview – jrc Oct 17 '15 at 00:19

1 Answers1

13

You need to override canPerformAction:withSender: and return NO for the actions you don't want.

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(_myCustomActionSelector:)) return YES;
    return NO;
}
axiixc
  • 1,962
  • 18
  • 25
  • 1
    This doesn't work for me - actions that I want to hide not even passed to this method (you can test it via NSLog(@"%@", NSStringFromSelector(action)); before returing. - iOS 7 and 8 – Andrei Konstantinov Feb 04 '15 at 06:32
  • If you aren't being asked about a particular selector it probably means there is something ahead of you in the responder chain that is accepting it. `-canPerformAction:withSender:` is called on the current first responder, and if it returns `NO` the next responder is asked. If any responder returns `YES` the next responder is not consulted. – axiixc Feb 11 '15 at 05:41
  • Thanks, axiixc, I need to subclass my UI element and write this method in .m file of that sublass. It works. – Andrei Konstantinov Feb 11 '15 at 07:08
  • I tried this solution for WKWebView so that I can remove the system callouts like copy, lookup, and share. This solution didnt work, these events never came – anoop4real Jun 23 '17 at 10:21
  • With WKWebView, I'm not sure there is a good way to do this. The WKWebView itself isn't the first responder, there is a deeper object which takes this role and will respond before higher responder have a chance. – axiixc Jun 23 '17 at 22:56