1

我已经实现了一个 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 后,如果我长按它,菜单就不会出现。为什么?

4

2 回答 2

1

我有同样的问题。在此答案中找到解决方案:您必须在 UITableViewController 的 viewDidAppear 中调用 becomeFirstResponder。

于 2013-01-29T10:00:53.537 回答
1

您应该实现以下UITabBarControllerDelegate方法:

迅速:

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

目标-C:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    [viewController becomeFirstResponder];
}
于 2017-09-29T02:36:33.013 回答