【问题标题】:In iOS7 UITabbarController, how to stop resetting the navigationController to root on tapping the already selected tabBarItem?在 iOS7 UITabbarController 中,如何在点击已选择的 tabBarItem 时停止将 navigationController 重置为 root?
【发布时间】:2013-11-01 17:38:55
【问题描述】:

我有一个 UITabBarController 作为 iPad 应用故事板中的 rootViewController。

它包含 3 个 tabBarItems。

每个item都有一个navigationController。所以总共有3个navigationController。

场景:

  1. 我选择了第二个 tabBarItem。然后第二个 navigationController 将在 tabBarController 上可见,并显示其关联的 rootController。

  2. 我在这个可见的 navigationController 上推送了一些控制器。

  3. 现在,当我点击第二个 tabBarItem(现在已经被选中)时,tabBarController 会弹出所有推送的控制器并将 navigationController 带到它的 rootController 视图。

问题: 我怎样才能阻止这种行为?当用户再次点击它时,选定的 tabBarItem 不应执行任何操作。

【问题讨论】:

    标签: ios iphone objective-c ipad


    【解决方案1】:

    如果您在 rootViewController 中初始化 UITabBarController,则在 rootViewController 中添加 UITabBarControllerDelegate,并实现此委托:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    {
        UIViewController* selected = [tabBarController selectedViewController];
    
        if (viewController == selected)
            return NO;
        else
            return YES;
    }
    

    【讨论】:

      【解决方案2】:

      为您的 UITabBarController 设置委托以达到您想要的任何行为。 (UITabBarControllerDelegate)

      然后实现委托方法

      - (id <UIViewControllerAnimatedTransitioning>)tabBarController:(UITabBarController *)tabBarController
                  animationControllerForTransitionFromViewController:(UIViewController *)fromVC
                                                    toViewController:(UIViewController *)toVC{
          return self;
      }
      -(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
      
          return 0.25f;
      }
      
      -(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
      
          self.transitionContext = transitionContext;
      
        //Custom transition method.
      
      
          [self executePresentationAnimation:transitionContext];
      }
      

      参考:https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarControllerDelegate_Protocol/Reference/Reference.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-08
        • 1970-01-01
        • 2016-02-26
        • 1970-01-01
        • 2010-12-05
        • 1970-01-01
        相关资源
        最近更新 更多