Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get ViewController from TabBarController on AppDelegate?

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...

like image 834
Ariel Zehao Zhang Avatar asked May 21 '12 16:05

Ariel Zehao Zhang


2 Answers

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
     }
}
like image 86
Gurjinder Singh Avatar answered Oct 10 '22 01:10

Gurjinder Singh


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];
like image 32
Dima Avatar answered Oct 10 '22 01:10

Dima