How to Configure Tab Bar Controller?

Asked

Viewed 312 times

1

I have a Tab Bar Controller with 5 Viewcontrollers connected to it.

With this, 5 buttons are displayed on the bar. I would like the Viewcontroller connected to the third button (middle button) to be the first to be displayed when the screen opens.

I built it through Storyboard.

3 answers

1

By storyboard you won’t be able to select another item. I suggest you select it by code, when the application is launched, on - application:didFinishLaunchingWithOptions: with the code below:

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

1

As your Tab Bar Controller will call the first screen that is connected to it automatically just by this code on viewDidLoad() from 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; 
    ...
}

0

The two answers are great! And they helped. But there are some observations that I find interesting to make. The @Douglasferreira solution, make a new instance of objects already created by Storyboard. And since I have a few screens before loading the Tabbarcontroller, then I wouldn’t act on the correct object.

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

The solution I created was the following:

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

[self setSelectedIndex:2];

And it worked!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.