切换到其他选项卡后,UIMenuItem不会出现

时间:2012-11-26 03:24:40

标签: ios uitabbar uimenuitem

我已经实现了一个UIMenuItem,通过长按TableViewController上的一个项来显示,它是UITabBarController的一个元素。  我就像下面那样做了

- (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
}

最初菜单显示效果很好。但是在切换到UITabBarController中的其他选项卡然后再切换回UITableViewController后,如果我长按它,菜单就不会出现。为什么呢?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。在this answer中找到解决方案:您必须在UITableViewController的viewDidAppear中调用becomeFirstResponder。

答案 1 :(得分:1)

您应该实施以下UITabBarControllerDelegate方法:

夫特:

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

目标-C:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [viewController becomeFirstResponder];
}