UIMenuController not getting displayed

2019-04-10 16:44发布

I am displaying UIMenuController on long press & double tap gesture, but its not getting displayed. I have followed the answers given here but it still not showing up. UIMenuController not showing up

Below is my code

- (void) doubleTap:(UITapGestureRecognizer *)gesture
{
    [self becomeFirstResponder]; 

    CGPoint location = [gesture locationInView:[gesture view]];
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"DoubleTap" action:@selector(hideKeyboard)];

    [menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
    [menuController setTargetRect:CGRectMake(location.x, location.y, 0.0f, 0.0f) inView:[gesture view]];
    [menuController setMenuVisible:YES animated:YES];
}

- (void) longPress:(UITapGestureRecognizer *)gesture
{
    [self becomeFirstResponder]; 

    CGPoint location = [gesture locationInView:[gesture view]];
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"LongPress" action:@selector(hideKeyboard)];

    [menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
    [menuController setTargetRect:CGRectMake(location.x, location.y, 0.0f, 0.0f) inView:[gesture view]];
    [menuController setMenuVisible:YES animated:YES];
}

- (BOOL)becomeFirstResponder
{
    return YES;
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:))
    {
        return YES;
    }
    return [super canPerformAction:action withSender:sender];
}

I am trying to get this event on didSelectRow of tableView, What can be worng.

1条回答
手持菜刀,她持情操
2楼-- · 2019-04-10 17:05

I finally solved my issue by adding below method in my code

- (BOOL) canBecomeFirstResponder 
{
     return YES; 
}

You must also implement the actions for all the buttons. Else that particular button wont be displayed in the menu.

查看更多
登录 后发表回答