Programatically switching tabs using selectedViewController property

我只是一个虾纸丫 提交于 2019-12-06 12:54:38

问题


I've used search already, haven't found an answer.

Trying to switch like this:

[self. tabBarController.selectedViewController OptionsViewContorller];

and like this:

 [self.tabBarController.selectedViewController = self.tabBarController.viewControllers     objectAtIndex:3];

but it doesn't work, i also tried and advice to change

self.tabBarController.selectedIndex

but it only changes at tab bar not a view.


回答1:


This should work.

self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];



回答2:


// this code i am using to switch to tabbar view controller 0, first view controller.

self.tabBarController.selectedIndex = 0;

UIViewController *controller = [self.tabBarController.viewControllers objectAtIndex:0];
if ([controller isKindOfClass:[UINavigationController class]]) {
    [((UINavigationController*)controller) popToRootViewControllerAnimated:false];
}

[self.navigationController popToRootViewControllerAnimated:true];



回答3:


// viewControllerIndex is an int describing the position of the viewController
// in the tab bar array index
[self.tabBarController setSelectedIndex:viewControllerIndex];



回答4:


If you want to switch from your UITabBarController class, you have to write this code in -viewDidAppear:animated:

[((UIViewController *) self.viewControllers[0]).tabBarController setSelectedIndex:1];

Hope this help.




回答5:


for swift 4+

you have to get reference to the tab before the view was presented

let tab = self.presentingViewController as! UITabBarController
self.dismiss(animated: true, completion:{ 
     tab.selectedIndex = 2
})


来源:https://stackoverflow.com/questions/9633420/programatically-switching-tabs-using-selectedviewcontroller-property

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