1

I have been implemented an UIMenuItem to show by long-pressing an item on a TableViewController that is an element of a UITabBarController. I did that like below

- (void)viewDidLoad
{
    resendMenuItem = [[UIMenuItem alloc] initWithTitle:@"Kirim Ulang" action:@selector(resend:)];
    [[UIMenuController sharedMenuController] setMenuItems: @[resendMenuItem]];
    [[UIMenuController sharedMenuController] update];

} 

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

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    selectedIndex = indexPath.row;
    return (action == @selector(resend:));
}

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    selectedIndex = indexPath.row;
    return YES;
}


-(BOOL) canPerformAction:(SEL)action withSender:(id)sender {
    return (action == @selector(resend:));
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

/// this methods will be called for the cell menu items
-(void) resend: (id) sender
{
 // do something
}

Initially the menu appears well. However after switching to other tab in a UITabBarController then switch back again to UITableViewController, the menu becomes not to appear if i long-press it. Why?

M Rijalul Kahfi
  • 1,460
  • 3
  • 22
  • 42
  • what happens if you move your code from viewDidLoad to viewDidAppear? I am guessing somethings gets knocked off when you switch tabs, and since the viewcontroller is loaded, your UIMenuController is not expecting to be shown. – Nitin Alabur Nov 26 '12 at 04:07
  • I'm still waiting for other answers.. – M Rijalul Kahfi Dec 12 '12 at 03:02

2 Answers2

1

I had the same problem. Found solution in this answer: You have to call becomeFirstResponder in the viewDidAppear of your UITableViewController.

Community
  • 1
  • 1
Marcel Wolterbeek
  • 3,367
  • 36
  • 48
1

You should implement following UITabBarControllerDelegate method:

Swift:

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    viewController.becomeFirstResponder()
}

Objective-C:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [viewController becomeFirstResponder];
}
geek1706
  • 980
  • 9
  • 16