Disable tab bar navigating to root view

為{幸葍}努か 提交于 2019-12-06 01:56:46

问题


I have a tab bar based application with navigation on each tab bar item. When i navigate to another view on any tab bar item and click on on tab bar item,then root view controller on that tab bar item is called. Its like PopToRootView . Can we disable this situation?


回答1:


Yes, you can disable the automatic popToRootViewController by implementing the UITabBarControllerDelegate method on your view controller:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if(self.navigationController == viewController) {
        return NO;
    }
    return YES;
}

Thanks to: Disable action - user taps on tabbar item to go to root view controller




回答2:


Though they say you're not supposed to subclass UINavigationController, you can what you want by making a subclass of UINavigationController and overriding the - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; method.

Doing this (and not calling the super popToRootViewController) will prevent the view controllers from popping when you click the tab bar item. It could run you into some problems somehow, but hopefully it works for you.




回答3:


  1. Include UITabBarControllerDelegate in your header file.
  2. try this:

    - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
      {
          if(bool_youDontWantPopToRootView)
               return (tabBarController.selectedViewController != viewController);
          return YES;
      }
    

For bool_youDontWantPopToRootView, you can add condition into it when you want it to behave like default behaviour.




回答4:


- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated; when you pass 'NO' in animated argument you will be directed to root view without any animation

you dont need to use this method while you tap on any tab bar its the default behaviour of the navigation controller that it maintain its own stack of VCs



来源:https://stackoverflow.com/questions/2056746/disable-tab-bar-navigating-to-root-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!