【问题标题】:In an UINavigationController inside of a TabBarController selecting the already selected tabBarItem pops the view to rootViewController, howTo change?在 TabBarController 内的 UINavigationController 中,选择已选择的 tabBarItem 会将视图弹出到 rootViewController,如何更改?
【发布时间】:2012-08-22 03:10:20
【问题描述】:

我在 TabBarController 中有一个 UINavigationController,当我选择已选择的 tabBarItem 时,NavigationController 会弹回到它的 rootViewController。 据我所知,这是一种自动行为。

我需要修改这种行为,并使用

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

再次推送我想要的viewController,由于我的推送与自动弹出并行发生,因此无法正常工作。

【问题讨论】:

  • 再次推送我想要的viewController?你在任何星球大战电影中玩过吗?也许像尤达?开个玩笑:请重新表述不清楚...
  • 你到底想做什么?
  • 我搜索了ty CSmith,但没有找到您链接的那个。

标签: ios uinavigationcontroller uitabbarcontroller


【解决方案1】:

我不知道如何改变这种行为,但我强烈建议你不要乱用这种默认行为。如果 Apple 没有提供一种简单的方法来解决这种行为,那么这是有原因的。

从字面上看,每个画外音用户都依赖手势,我假设其中之一是双击一个选项卡以转到 rootView。使用这些默认手势中的任何一个,您肯定会激怒一群 VO 用户。不幸的是,我学会了这一点(使用自定义导航栏和自定义后退按钮)。

希望这个答案能说服您重新考虑您的要求 =)

【讨论】:

    【解决方案2】:

    解决方案是将 UINavigationController 子类化,并将您的子类与 UITabBarController 一起使用。我加入了其他几个有用的功能。

    这样做很好 - 我的应用程序有 5 颗星并且没有人抱怨它:

    @implementation MyNavigationController
    
    // This suppresses the normal pop to the root view controller
    - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
    {   
        return @[];
    }
    
    // Extra: give my base classes some notice this is going to happen
    - (UIViewController *)popViewControllerAnimated:(BOOL)animated
    {
        UIViewController *vc = self.topViewController;
        if ([vc respondsToSelector:@selector(viewControllerWillBePopped)]) {
            [vc performSelector:@selector(viewControllerWillBePopped)];
        }
    
        return [super popViewControllerAnimated:animated];
    }
    
    // Extra: let the UIViewController refuse to pop
    - (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
    {
        BOOL ret = YES;
    
        UIViewController *vc;
        for(UIViewController *obj in self.viewControllers) {
            if(obj.navigationItem == item) {
                vc = obj;
                break;
            }
        }
    
        if ([vc respondsToSelector:@selector(shouldPop)]) {
            NSNumber *retVal = [vc performSelector:@selector(shouldPop)];
            ret = [retVal boolValue];
            if(!ret) return NO;
        }
        return [super navigationBar:navigationBar shouldPopItem:item];
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2019-07-08
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2019-06-21
      • 2011-11-30
      • 1970-01-01
      • 2011-01-09
      • 2012-02-11
      相关资源
      最近更新 更多