UISplitview: access to UITabBarController from appDelegate

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

Storyboard with UISPlitViewCOntroller as root

This is what I 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 application crashes, complaining that the tabController is zero. If I remove the UINavigation controller from the storyboard, the UITabBarController is accessed using:

  let tabController = splitViewController.viewControllers.first as! UITabBarController 

What is the correct way to access the UITabBarController child controllers where the UISplitView is the root?

+5
source share
2 answers

Finally, I found a solution. I had to use the "childViewControllers" of the navigation controller as follows:

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

+4
source

Instead of embedding the tab bar controller in the navigation controller, you should embed the child controllers in your own navigation controllers, for example:

 Split View -> Tab Bar -> Navigation Controller #1 -> View Controller -> Navigation Controller #2 -> View Controller 

This is the right way to use the tab bar in combination with the navigation controller.

+1
source

Source: https://habr.com/ru/post/1243505/


All Articles