UITabbarController selectedIndex and selectedViewController do not work

你离开我真会死。 提交于 2019-12-22 12:57:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!