Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change the Tab (of a UITabViewController) Programmatically?

I'm trying to programatically select/change the tab of the UITabViewController.

I tried doing it via:

self.tabBarController.selectedIndex = 2;

This looks to be the way that I should do it, but it doesn't work. I thought that maybe the self.tabBarController returns a read only object (I sorta remember reading that somewhere), but I'm not sure how to get it to work.

Any help is appreciated!

like image 591
rksprst Avatar asked Feb 18 '09 17:02

rksprst


People also ask

How do I change tab bar name?

The site title that appears in the tab bar can be changed from Appearance > Customize > Site Identity.


3 Answers

Set selectedViewController:

self.tabBarController.selectedViewController = viewControllerYouWant;

For example,

self.tabBarController.selectedViewController 
    = [self.tabBarController.viewControllers objectAtIndex:2];
like image 143
Himadri Choudhury Avatar answered Nov 15 '22 13:11

Himadri Choudhury


According to the docs, selectedIndex or selectedViewController are the properties you want and are both assignable.

The tabBarController property is readonly, but it returns an object that is editable.

So all your code looks right to me.

like image 35
Alex Wayne Avatar answered Nov 15 '22 14:11

Alex Wayne


This works well for me, if you want to do it by index.

[self.tabBarController setSelectedIndex:2];
like image 36
d0ugal Avatar answered Nov 15 '22 13:11

d0ugal