Open view in UITabBarController
I need to open a programmatically defined tab in my tab bar.
I tried to call the following method from my UITabBarController:
self.selectedViewController = myVController;
What does not work.
What's the correct way to open a tab?
Thanks.
Update: I tried the following code:
self.selectedViewController = [ [self viewControllers] objectAtIndex: 0];
which opens me the tab I want (it gets selected) but the active view is not refreshed.
source to share
As for the property selectedIndex
, Apple says:
In versions of iPhone OS prior to 3.0, this property reflects the index of the selected tab bar item only. Attempting to set this value to the index of a view controller that does not appear in the tab bar but is instead navigated by the More 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 receive item selection messages and manually switch views. (Though, if you're using advanced features UITabBarController
, it might not be worth your effort.)
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
}
source to share