6

I'm looking to disable the functionality of pressing my tab bar and returning to the root view in the navigation hierarchy whilst also keeping this functionality for a button in my navigation bar.

So the only way I want the user to go back to the root view is by pressing the button in the navigation bar and not by tapping the tab bar button.

I've had a look around and tried several solutions but none seem to work as they disable the functionality for both the nav bar button and the tab bar button but not just the tab bar button.

Thanks!

ryder
  • 125
  • 2
  • 12
  • I don't know what functionality you're talking about. When you switch tabs in a tab bar controller, it should have no effect on the navigation stack of any of the navigation controllers that are root controllers in a tab. I certainly haven't seen that behavior in my apps. Explain what your controller setup is. – rdelmar Feb 28 '15 at 16:14
  • 3
    He doesn't mean switching between tabs, as far as I understand. When you select the currently selected tab (again) in your UITabBarController it automatically pops back to the rootviewController of its navigationController stack. That's the default behavior. And he wants to avoid this. Correct me if I'm wrong. – croX Feb 28 '15 at 16:24
  • @croX, yeah, I misunderstood the question. What you describe is the default behavior when you click on the tab you're already on. – rdelmar Feb 28 '15 at 16:36
  • @rdelmar, yes exactly. – croX Feb 28 '15 at 16:37
  • HI guys, yes apologies that wasn't clear. It is when the tab is already selected and pressed again – ryder Mar 01 '15 at 22:49
  • I am having the exact same problem. Surprised I found this post – Nearpoint Mar 03 '15 at 15:28
  • 1
    I found this solution and it works really well: http://stackoverflow.com/questions/4856159/double-clicking-on-uitabbarcontrollers-tab-goes-to-root-of-navigation-controller – Nearpoint Mar 03 '15 at 16:26

2 Answers2

4

A possibility would be to create a subclass of UITabBarController and to implement the UITabBarControllerDelegate protocol. Then you could implement

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

and return NO, when the particular viewController is selected again. A way to do this is is to save the recently selected item and to compare it with the currently selected one.

croX
  • 1,605
  • 1
  • 17
  • 23
0

Came across this issue at the weekend. I kept finding my custom TabBarController was nil during appDelegate didfinishlaunching() method.

The way I got around it was to make my custom TabBarController a UITabBarControllerDelegate and then implemented the following delegate method withint he custom TabBarController class:

    // Stops View Controllers from being dismissed when a tab is selected on the UITabBarController
    public func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    return viewController != tabBarController.selectedViewController
    }
Charlie Seligman
  • 3,508
  • 4
  • 50
  • 84