1

I am trying to display UIMenuController when user selects any row in the table. I am using UITableViewController to display table with custom cell.

my code:-

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

    MyCell *cell = (MyCell *)[tableView cellForRowAtIndexPath:indexPath];
    CGRect cellFrame = cell.frame;

    [self.view becomeFirstResponder];

    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Item1" action:@selector(action1:)];
    UIMenuItem *menuItem1 = [[UIMenuItem alloc] initWithTitle:@"Item2" action:@selector(action2:)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Item3" action:@selector(action3:)];

    UIMenuController * menuController = [UIMenuController sharedMenuController];
    menuController.menuItems = [NSArray arrayWithObjects:menuItem, menuItem1, menuItem2, nil];
    menuController.arrowDirection = UIMenuControllerArrowDown;

    [menuController setTargetRect:cellFrame inView:self.view];

    [menuController setMenuVisible:YES animated:YES];
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

But UIMenuController doesn't shown. Whats wrong in above code ?

Also, I've referred these links. But no luck.

Maulik
  • 19,348
  • 14
  • 82
  • 137
  • hey bro.....refer this link if you have not..!!!http://www.intridea.com/blog/2010/12/22/developers-notes-for-uimenucontroller# – NiravPatel Jan 23 '13 at 07:45
  • See this, may be helpful. http://stackoverflow.com/questions/4582116/copy-paste-functionality-in-uitableviewcontroller – Cynichniy Bandera Jan 23 '13 at 08:42

1 Answers1

0

If you're ok with showing the menu only after a long press, there is no need to use tableView:didSelectRowAtIndexPath: and show the menu yourself.

Instead, you can use this delegate method:

- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

To hide the standard items (cut, copy, and paste), return NO here:

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

Then you need to return YES from canBecomeFirstResponder like you have and, for some reason, I had to implement this method too:

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}
gerry3
  • 21,420
  • 9
  • 66
  • 74
  • Determine when a UIMenuController is dismissed? – jose920405 May 21 '15 at 13:41
  • This notifications ```[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willHideEditMenu:) name:UIMenuControllerWillHideMenuNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didHideEditMenu:) name:UIMenuControllerDidHideMenuNotification object:nil];``` Not work using this delegates – jose920405 May 21 '15 at 13:44