问题
I've tried to put following two lines into - (void)viewDidLoad.
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
At that time, only Tabbar index to change 3 but View Controller remained at default view controller, mean 0. Is there any thing missing in my coding?
回答1:
Solution: Try by moving your,
self.tabBarController.selectedIndex = 3;
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
portion of your code to UIVIewController's
- (void)viewDidAppear:(BOOL)animated
I hope it will help you
Reason: You have to let your UIViewController to load fully first before you trigger an extra action on it.
回答2:
Try this definitely it works:
UINavigationController *nc = [[self.tabBarController viewControllers] objectAtIndex:0];
id vc = [[nc viewControllers] objectAtIndex:0];
if ([vc isKindOfClass:[YourViewController class]])
{
[[self appDelegate].tabBarController setSelectedIndex:3];
}
来源:https://stackoverflow.com/questions/23966301/uitabbarcontroller-selectedindex-and-selectedviewcontroller-do-not-work