UIMenuController not getting displayed

我与影子孤独终老i 提交于 2019-12-06 06:48:17

问题


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:


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.



来源:https://stackoverflow.com/questions/16054050/uimenucontroller-not-getting-displayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!