Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISplitview: access UITabBarController from appDelegate

I need to access the UITabBarController, and the second of its subviews from the appDelegate.

Storyboard with UISPlitViewCOntroller as root

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?

like image 851
Tom Tallak Solbu Avatar asked Feb 21 '16 13:02

Tom Tallak Solbu


2 Answers

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 :-)

like image 118
Tom Tallak Solbu Avatar answered Nov 20 '22 19:11

Tom Tallak Solbu


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.

like image 1
Tim Vermeulen Avatar answered Nov 20 '22 17:11

Tim Vermeulen