Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove default menu items from UIMenuController?

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];
}
like image 644
Perchouli Avatar asked Dec 11 '22 15:12

Perchouli


1 Answers

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;
}
like image 186
axiixc Avatar answered Jan 11 '23 20:01

axiixc