Reputation: 559
I need to access the UITabBarController, and the second of its subviews from the appDelegate.
This is what I have tried in applicationDidEnterBackground:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let tabController = leftNavController.tabBarController! as UITabBarController
let controllers : Array = tabController.viewControllers!
print("viewcontrollers \(controllers)")
The app crashes, complaining that tabController is nil. If i remove the UINavigation controller from storyboard, the UITabBarController is accessed easily with:
let tabController = splitViewController.viewControllers.first as! UITabBarController
What is the correct way to access the childcontrollers of the UITabBarController, where a UISplitView is the root?
Upvotes: 5
Views: 1107
Reputation: 559
Finally I found a solution. I had to use "childViewControllers" of the navigation controller like this:
let splitViewController = self.window!.rootViewController as! UISplitViewController
let leftNavController = splitViewController.viewControllers.first as! UINavigationController
let tabController = leftNavController.childViewControllers.first as! UITabBarController
let viewControllers : Array = tabController.viewControllers!
print("viewControllers \(viewControllers)")
Now I can easily access any of the viewControllers and run their methods from appDelegate :-)
Upvotes: 4
Reputation: 12562
Rather than embedding your tab bar controller in a navigation controller, you should embed the child view controllers in their own navigation controllers, like this:
Split View -> Tab Bar -> Navigation Controller #1 -> View Controller
-> Navigation Controller #2 -> View Controller
This is the correct way to use a tab bar in conjunction with a navigation controller.
Upvotes: 1