Tom
Tom

Reputation: 1546

Tab+NavigationController flow

I've got a tabbar controller as a main screen in my app. Some of the tabs got navigation controller embedded.

Here is my problem: First tab is the initial one when app starts. Under certain conditions user should see second screen B (on navigation stack) immediately after app starts (there is performSegue which triggers in viewWillAppear of first screen). This works as it should. User starts the app and immediately sees the second screen. This also works when user switches to different tab and back. The problem is when user already is on the first tab and taps on it again. Then the stack gets destroyed users sees first screen A which will animate into second screen B in short order. This transition is clearly visible by user.

First tab --> screen A --> screen B --> ...
|
Second tab --> screen T --> screen U --> ...
|
...

So the question is how to prevent this behaviour? User shouldn't see the transition between A and B in this case.

Thanks

Upvotes: 0

Views: 43

Answers (2)

sonxurxo
sonxurxo

Reputation: 5718

That's the expected behavior of UITabBarController, it will pop the navigation controller to the first view controller in its stack.

If you don't want the screen A to appear under certain conditions when user taps the tab bar item, just remove the first view controller from the navigation stack, doing something like this after pushing from A to B (form instance in the viewDidLoad: of B view controller):

NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: navigationController.viewControllers];
[allViewControllers removeObjectAtIndex: 0];
navigationController.viewControllers = allViewControllers;

This way screen B will be the root view controller of your navigation stack, which is what you want.

Upvotes: 0

Viral Savaj
Viral Savaj

Reputation: 3389

Why don't you change your code from Screen A to perfrom segue to next next Screen B unser CERTAIN condition.,

Just use that CERTAINcondition in TabbarController Class ( Subclass of Tabbar) in method like,

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 0)
    {  
         if (CERTAIN condition True) {
              UITabBar tabbar = self.tabBarController.tabBar;
              NSArray *controlrs = self.viewControllers; 
              NSMutableArray *controllerCopy = [controlrs mutablecopy];
              SCREEN_B_class *bClassObj =  . .. //Tab 1 class Just Screen B, not Screen A -> ScreenB
              [controllerCopy replaceObjectsAtIndex:[NSIndexSet indexSetWithIndex:0] withObjects:[NSArray arrayWithObjects:bClassObj]]
              tabBarController.viewControllers = controllerCopy;

         }

    }
}

Hope this helps you,

HTH, Enjoy Coding!!

Upvotes: 1

Related Questions