Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop tab bar's second tap which pops to navigation controller? [duplicate]

I have a tab bar based app. All tabs have a navigation controller as the root. If the user taps on the tab again if the tab is active, it pops back to the navigation controller.

How can I stop this behavior?

So in fact I have a navigation controller + a hidden viewcontroller that makes some decisions + another view controller. Sorry for the misleading information in the original question. I use the hidden viewcontroller for all the tabs, 3 of them, since I have the login screen on all 3 if the user is not logged in. If the user logs in, then I pop the login screen, and put the 1,2,3 individual viewcontrollers on each tab.

First tap:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 did disappear  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  

Second tap:

 0 : class=Crossing: 0x645c8a0>  
 1 : class=FavoritesViewController: 0x64ac140>  
 shouldSelectViewController : UINavigationController  
 UINavigationController topclass:FavoritesViewController  
 myTabBarController.selectedViewController :UINavigationController  
 didSelectViewController : UINavigationController  
 UINavigationController topclass:Crossing  
like image 879
Zsolt Avatar asked Oct 26 '25 18:10

Zsolt


2 Answers

@MarkGranoff was on the right track for doing this, but the way to do it is by doing something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([tabBarController.viewControllers indexOfObject:viewController] == tabBarController.selectedIndex)
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

or in a less verbose way:

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

If you only want to block the default behaviour for a certain tab then you can do something like this:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSUInteger indexOfNewViewController = [tabBarController.viewControllers indexOfObject:viewController];
    // Only the second tab shouldn't pop home 
    return ((indexOfNewViewController != 1) ||
            (indexOfNewViewController != tabBarController.selectedIndex));
}
like image 176
hypercrypt Avatar answered Oct 29 '25 07:10

hypercrypt


Swift 3

  1. Subclass your UITabBarController and implement UITabBarControllerDelegate on that class.

    class viewMain: UITabBarController, UITabBarControllerDelegate {

  2. In viewDidLoad, set your class's delegate to be itself

    self.delegate = self

  3. Add this function

    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { return (viewController != tabBarController.selectedViewController); }

like image 42
Brad C Avatar answered Oct 29 '25 09:10

Brad C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!