0
votes

My app has tab bar controller that loads 3 view controllers in its viewDidLoad method.

- (void)viewDidLoad
{
    [super viewDidLoad];
    ...
    [self setViewControllers:@[firstViewController,
                               secondViewController,
                               thirdViewController,
    ]];
}

I want it to show up with a view controller (homeViewController) that is different from these three controllers. When the tab bar is first loaded none of these three tab bars will be selected. I want to change them by pressing tab bar items and return to home view by pressing navigation left bar button.

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:btnImage
                                                                         style:UIBarButtonItemStylePlain
                                                                        target:self
                                                                        action:@selector(setHomeView)]; 

How can I show the homeViewController when the tab bar controller is first loaded without added it to tab bar items?

1
The UITabBarController is not meant to be used this way. Each UITabBarController essentially will have it's own UINavigationController in most cases and hence, their own stack. These tabs are all separated from one another. If I go to firstViewController, then get pushed to another VC, then click on the second tab, I no longer have the navigation left bar button to get back to the home VC. You're not meant to be able to be on a Home VC, then go to Tab 1, then tab 3, and still have a back button to get back to the home VC. Rethink your UI/UX flow.LyricalPanda
all these three view controllers inherited from same base class so that they have same navigation left bar buttonzontragon
But they won't have the same navigation stack. The UITabBarController is meant to swap the rootViewController with firstVC, secondVC, thirdVC. Each of these will have different navigation stacks, the only way they carry over is if you do self.navigationController.push. The problem with doing this then is I can do -> HomeVC -> 1st -> 3rd -> 1st -> 3rd -> 1st -> 3rd -> 1st ->3rd. Now I have to hit the back button 9 times to get back to the home screen. This also takes up a lot of memory and you don't restore any of the previous stack you could have had.LyricalPanda
I see. Is there a another way to achieve this kind of navigation?zontragon
Truthfully, most people just have HomeVC added to the UITabBarController to get around this issue so they can do self.window.rootViewController = {homeVC,firstVC,secondVC,thirdVC}. Otherwise if it's just a landing screen they don't care about being able to go back to it once they are past it.LyricalPanda

1 Answers

0
votes

The tab bar controller by default sets the first ViewController at index0 to be the home controller.

If you don't want the first ViewController of the application to live inside of the tabBarController then you have to create your HomeVC and set it as the entry point for the application, but the tab bar won't be present in that View.

Theres no way to present a view controller inside of the TabBarController unless it lives somewhere inside of those three navigation stacks.