1
votes

So I'm attempting to deal with uitabbarcontroller and wanted to have a default tab be selected when the view loads.

I have tried setting the

@property (nonatomic) int selectedIndex;

however this just sets which viewcontroller is being shown, not the actual tabbar being selected

I also tried this line

[self tabBar].selectedItem = [[[self tabBar] items] objectAtIndex:0];

but it crashes as you are not allowed to mutate the tabbar of a tabbarcontroller

Any help with this?

Thanks

1
still having issues with this - trying to mimmic the app store app and how it auto selects the middle tab or whatever the last tab was. really dont wanna have to create my own tabbar under my own viewcontroller if possible - DanZimm
You say that something like [self.tabBarController setSelectedIndex:2]; is not what you need, right? Can you explain a bit more what do you mean by "not the actual tabbar being selected"? - phi
The physical view changes to the correct viewcontroller's view but the uitabbar doesnt change which tab is selected - DanZimm
That's weird - the documentation suggests that selectedIndex is The index of the view controller associated with the currently selected tab item. and in my quick test it worked as it should. Could it be something else? Can you post more code maybe? - phi
oops I guess I called setSelectedIndex at the wrong time. I was calling it in the initializer. if you submit an answer ill mark it as right, thanks! - DanZimm

1 Answers

3
votes

So, as discussed in the comments, following code should do what you want:

[self.tabBarController setSelectedIndex:desiredIndex];

Alternatively, you can use:

[self.tabBarController setSelectedViewController:desiredViewController];