views:

315

answers:

2

Hi,

I need to open programatically a certain tab in my tab bar.

I tried calling the following method from my UITabBarController:

self.selectedViewController = myVController;

Which doesn't work.

What is the right way to open a tab?

Thank you.

Update: I tried the following code:

self.selectedViewController = [ [self viewControllers] objectAtIndex: 0];

which opens me a desirable tab(it is being selected), but the active view is not being updated.

A: 

The way I've done it is to use self.tabBarController.selectedIndex = 1 (first tab is at index 0).

Alistair
It has the same problem that my code had. The view is not being always updated.
Ilya
+1  A: 

Regarding the selectedIndex property, Apple's docs say:

In versions of iPhone OS prior to version 3.0, this property reflects the index of the selected tab bar item only. Attempting to set this value to an index of a view controller that is not visible in the tab bar, but is instead managed by the More navigation controller, has no effect.

Perhaps this was your problem?

One possible workaround is to use your own UITabBar and handle your own views. Then you can do something like this:

[tabBar setSelectedItem:[tabBar.items objectAtIndex:1]];

Just implement UITabBarDelegate and define this function to get item selection messages and manually switch views. (Although if you are using advanced UITabBarController features it may not be worth your effort.)

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{ }
Nathan S.