Search code examples
iphoneuimenucontrolleruimenuitem

UIMenuController not getting displayed


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.


Solution

  • 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.