How to Configure Tab Bar Controller?

1

I have a Tab Bar Controller with 5 ViewControllers attached to it.

This brings up 5 buttons on the bar. I would like the ViewController attached to the third button (middle button) to be the first to be displayed when the screen is opened.

I built it through the Storyboard.

    
asked by anonymous 01.05.2015 / 22:59

3 answers

1

Through the storyboard you will not be able to select another item. I suggest you select it by code, when the application is launched, in - application:didFinishLaunchingWithOptions: with the code below:

UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
UIViewController *terceiroViewController = tabController.viewControllers[2]; // Indice do VC desejado
tabController.selectedViewController = terceiroViewController;
    
02.05.2015 / 12:14
1

As your Tab Bar Controller will call the first screen that is attached to it automatically suffices for this code in viewDidLoad() of the first screen:

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    //como são 5 botões o index do terceiro é 2.
    self.tabBarController.selectedIndex = 2; 
    ...
}
    
02.05.2015 / 19:23
0

The two answers are great! And they helped. But there are some observations that I find interesting to do. The @DouglasFerreira solution, make a new instance of objects already created by the Storyboard. And since I have some screens before loading the TabBarController, then it would not act on the correct object.

The solution presented by @iTSangar did not work because the object did not respond to direct pointing in the variable selectedIndex .

The solution I created was as follows:

I created a controller class and called it with the TabBarController in the Storyboar, and in the method   viewDidLoad , as recommended by @iTSangar, I did this:

[self setSelectedIndex:2];

And it worked !!

    
03.05.2015 / 19:49