How can I get ViewController from TabBarController on AppDelegate?
Solution 1
You said that your initial (root) view controller is a UITabBarController but you are referring to a view controller with a navigation controller with a tab bar controller. Are you getting mixed up in your view controller hierarchy?
edit:
if your root view controller is actually just a tab bar controller and you want to get the 3rd tab here is the code:
[[((UITabBarController *)self.window.rootViewController) viewControllers] objectAtIndex:2];
Solution 2
[self.tabBarController setSelectedIndex:2];
Solution 3
Try this way
In any view controller
YourViewController *yourViewController= (YourViewController*) [self.tabBarController.viewControllers objectAtIndex:3];
this return your that view controller object.Cast it to your view controller and you are ready to use that. run and Go
Solution 4
Swift 4.0
let viewC = self?.tabBarController.viewControllers.first // will give single Navigation Controller on index 0
let viewC = self?.tabBarController?.viewControllers// will give array of Navigation Controller
Further you can check the Visible ViewController
if let nav = viewC as? UINavigationController {
if nav.visibleViewController is YourViewControllerName{
// Do Code
}
}
Solution 5
UIViewController *loginViewController=self.window.rootViewController;
UITabBarController *controller=loginViewController.tabBarControllerObj;
UIViewController *selectedController=controller.selectedViewController;
From this you will get selected view controller.
For getting all view controller just replace
NSArray *viewControllers = [controller viewControllers];
Ariel Zehao Zhang
• Over 6 years experience specializing in Java J2EE and 3 years experience in C# .NET(Azure-based application) • Knowledge of HTML, Javascript(AngularJS), Object-C(iOS app), RESTful Web Service • Active Member of User Experience standards effort • Technical leadership for large team project with remote team members, including design review and code review • Participated in both regular and agile software development life cycle • Good communicating skills with different roles • Passionate, self-driven and self-managing • Embrace change, love to learn new technology
Updated on February 07, 2020Comments
-
Ariel Zehao Zhang about 4 years
I use iOS5 storyboard, and my initial view controller is
TabBarController
, there are four relationships to view1 & view2 & view3 & view4, so, how can I get the view3's view controller?I tried:
[[[[self.window.rootViewController navigationController] tabBarController] viewControllers] objectAtIndex:2];
But it doesn't work...