Serguei Fedorov
Serguei Fedorov

Reputation: 7943

Setting SelectedIndex on tabBarController in viewDidLoad does not work

I am having an issue trying to set the current tab for the tabBarController in which I have two views nested. When the application starts, I want to change the default tab being displayed. As a result, I am doing this out of viewDidLoad of one of the nested viewcontrollers. I can disable the tabBarController by:

 self.tabBarController.tabBar.userInteractionEnabled = NO;

and the tabBarController has an address (it exists).

I have tried a few different variations of changing the tab including:

self.tabBarController.selectedIndex = 1;

and

[self.tabBarController setSelectedIndex:1];

however neither seem to work. They work, however, when I invoke these functions through IB actions.

This is being done out of one of the nested controllers. Maybe this has to be done out of somewhere else?

Upvotes: 1

Views: 1376

Answers (2)

YogiBhoi
YogiBhoi

Reputation: 169

Use the selectedIndex property on the UITabBarController.

 controller.selectedIndex = tabBarButtonIndex;

or use

[controller setSelectedIndex:tabBarButtonIndex];

hope this helps

Upvotes: 1

Phil Wilson
Phil Wilson

Reputation: 928

Make sure you're calling [super viewDidLoad] first

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tabBarController setSelectedIndex:1];
}

Upvotes: 0

Related Questions