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]; } 
1

1 Answer 1

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; } 
Sign up to request clarification or add additional context in comments.

5 Comments

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
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.
Thanks, axiixc, I need to subclass my UI element and write this method in .m file of that sublass. It works.
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
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.