7

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;
}
Davis Broda
  • 4,102
  • 5
  • 23
  • 37
  • this is viewDidLoad of TabBarController ? where is the done button? – Kunal Balani Oct 14 '13 at 20:58
  • please share your code in touch event of tabBarItem or touch delegate of tabBarContoller wherever your push logic is there for first view controller? – Kunal Balani Oct 14 '13 at 21:01
  • Hi Kunal, this is the viewDidLoad of FirstViewController an empty UIView Class. (@interface FirstViewController : UIViewController) Also I don't have any code in the touch event, since I'm adding all the controls programmatically in the appDelegate didFinishLaunchingWithOptions. – Gastón Algaze Oct 14 '13 at 21:07
  • so is there any code on selecting tab bar item ? – Kunal Balani Oct 14 '13 at 21:09

1 Answers1

12

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;
}
trydis
  • 3,905
  • 1
  • 26
  • 31
  • Ok, is there a way to override this behavior? Should I be implementing this differently? – Gastón Algaze Oct 14 '13 at 21:13
  • I think this behavior is for double tap , thats a different event than doing a single tap on selected one ? – Kunal Balani Oct 14 '13 at 21:22
  • Thanks for the reply, in order to apply that code, I should be implementing a custom tab bar controller and adding that delegate right? – Gastón Algaze Oct 14 '13 at 21:23
  • @KunalBalani He mentions that the "issue" occurs when touching the same tab bar item again. – trydis Oct 14 '13 at 21:24
  • @GastónAlgaze No, just set the delegate on the UITabBarController and implement the method. – trydis Oct 14 '13 at 21:26
  • @trydis, so I should be doing something like this: \\@interface AppDelegate : UIResponder – Gastón Algaze Oct 14 '13 at 21:33
  • Then your question is duplicate : http://stackoverflow.com/questions/6585899/tab-bar-second-tap-pops-to-navigation-controller-how-to-stop-it http://stackoverflow.com/questions/1849975/prevent-automatic-poptorootviewcontroller-on-double-tap-of-uitabbarcontroller – Kunal Balani Oct 14 '13 at 21:37
  • Thanks @KunalBalani I fixed it by adding the suggested code by trydis, and tabBarController.delegate = self to the appDelegate didFinishLaunchingWithOptions. Sorry if this was a duplicate question it really helped me to understand the topic, i didn't knew the terms that applied to this, for example popToRootViewController, probably this can help others as well. – Gastón Algaze Oct 14 '13 at 21:43