0

In my application, i've to show a menu controller on tapping a table view cell. Its showing a menu. As well as all the actions are being executed successfully. Good till now.
One minor problem I'm facing is that, I'm not able to hide the menu controller if the cell (or some other cell) is tapped again. Here's the code that I'm using:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    UIMenuController* menuController = [UIMenuController sharedMenuController];

    if ([menuController isMenuVisible])
    {
        [menuController setMenuVisible:NO animated:YES];
    }
    else
    {
        [self becomeFirstResponder];        
        self.selectedIndex = indexPath.row;
        [menuController setTargetRect:[tableView rectForRowAtIndexPath:indexPath] inView:tableView];

        [menuController setMenuItems:@[
            [[UIMenuItem alloc] initWithTitle:@"Play" action:@selector(playVideo:)],
            [[UIMenuItem alloc] initWithTitle:@"Edit" action:@selector(editVideo:)],
            [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteVideo:)],
            [[UIMenuItem alloc] initWithTitle:@"Share" action:@selector(shareVideo:)],
            [[UIMenuItem alloc] initWithTitle:@"Cancel" action:@selector(cancelMenu:)]
        ]];

        menuController.arrowDirection = UIMenuControllerArrowUp;
        [menuController setMenuVisible:YES animated:YES];
    }
}

I don't know why is it not hiding on tapping the table view cell again. Can some one guide me on what's the mistake that I'm doing?

Satyam
  • 15,493
  • 31
  • 131
  • 244
  • Have you tried `[self resignFirstResponder];` instead of `setMenuVisible:NO`... – jjv360 Mar 11 '13 at 11:14
  • The problem that I noticed is that the line of code "if ([menuController isMenuVisible])" is always returning NO. – Satyam Mar 11 '13 at 11:53

1 Answers1

0

In my experience, dismissing the menu controller with [menuController setMenuVisible:NO animated:NO]; has helped. I suppose if you try to animate a menu out and in in the same block of code you might run into problems.

architectpianist
  • 2,562
  • 1
  • 19
  • 27
  • I'm checking for the condition if its visible or invisible.... so do you still think that it will be an issue? First of all, its always telling me that menu is not visible. Some thing wrong that I'm doing. – Satyam Mar 11 '13 at 12:38
  • What happens if you take away the if clause and just use [menuController setMenuVisible:NO animated:NO]; ? – architectpianist Mar 11 '13 at 20:11
  • Simple, It will always shows the menu. My requirement is that on first tap, it will show the menu, on second tap it has to hide. – Satyam Mar 12 '13 at 01:26