Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tap twice on selected tab bar item hides the current view controller [duplicate]

I'm having some issues pushing a view, using the navigation stack.

The problem I encounter is that after touching a tab bar item a view controller is pushed into the navigation stack (from a view controller named FirstViewController) like so :

- (void)viewDidLoad
{
    [super viewDidLoad];
    svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];
}

That works as expected, but the actual issue arises when touching the same tab bar item again.

When that happens the current view ( the SecondViewController that was previously pushed) is removed, it's like I would be touching the "done" button.

I can not trace where or why that's happening.

EDIT: This is how I set up the tab bar, view controllers and navigation:

@implementation AppDelegate
@synthesize HomeViewController, FirstViewController, SecondViewController,     ThirdViewController, SettingsViewController, tabBarController, window;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    FirstViewController *firstViewController = [[FirstViewController alloc]
                                            initWithNibName:nil bundle:nil];
    UINavigationController *firstViewControllerNav = [[UINavigationController alloc]
                                      initWithRootViewController:firstViewController];

    SecondViewController *secondViewController = [[SecondViewController alloc]
                                             initWithNibName:nil bundle:nil];
    UINavigationController *secondViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:secondViewController];

    ThirdViewController *thirdViewController = [[ThirdViewController alloc]
                                                  initWithNibName:nil bundle:nil];
    UINavigationController *thirdViewControllerNav = [[UINavigationController alloc]
                                                      initWithRootViewController:thirdViewController];

    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[firstViewControllerNav,
                                              secondViewControllerNav];

    UITabBar *tabBar = tabBarController.tabBar;

    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];

    [self.window setRootViewController:self.tabBarController];

    [self.window makeKeyAndVisible];

    return YES;
}
like image 652
Gastón Algaze Avatar asked Oct 14 '13 20:10

Gastón Algaze


1 Answers

Touching a tab bar item twice will cause the navigation controller to pop back to the root view controller. This is expected and built-in behavior.

To prevent this you will have to use the tabBarController:shouldSelectViewController: method of the UITabBarControllerDelegate protocol.

I believe something like this will do the trick:

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return viewController != tabBarController.selectedViewController;
}
like image 198
trydis Avatar answered Oct 21 '22 19:10

trydis