Load All Tabbar Views

How to load all views in UITabBarController?

To preload a UIViewController's view, simply access its view property:

let _ = myViewController.view

To preload all view controllers on a UITabBarController, you can do:

if let viewControllers = tabBarController.viewControllers {
for viewController in viewControllers {
let _ = viewController.view
}
}

Or a bit more compactly:

tabBarController.viewControllers?.forEach { let _ = $0.view }

Load All TabBar Views

If you take your view initialization code and move it into loadView instead of viewDidLoad you can force each of the UIViewControllers that are part of your UITabBarController to be loaded by simply calling viewController.view. This happens because a UIViewController will create the view object via the loadView function when asked for it.

for(UIViewController * viewController in  tabBarController.viewControllers){
viewController.view;
}

or more simply

[tabBarController.viewControllers makeObjectsPerformSelector:@selector(view)];

Load all views related to tab bar controller by hard code

Oh, this code looks so wrong.

  1. In your storyboard give and "tabView" ID to the TabBarController, not the ViewController inside it.
  2. Why you are double instantiating ViewController? just do it once and assign it to the vc variable.
  3. Why you've created delay before presenting the VC? It's some sort of workaround of something?

Working code:

    let vc = storyboard.instantiateViewController(withIdentifier: "tabView") as! UITabBarController
self.present(vc, animated: true)

How can i load views of other tabs on the app launch?

To force the viewDidLoad method to be called, you simply need to reference the view property of the view controller.

Something like this would do it:

_ = someViewController.view // causes `viewDidLoad` to be called.

Wait to load child views in UITabBarController?

You could start with an empty tab bar controller and add the child view controllers programmatically as the API call completes.

However, this begs the question, why show the tab bar controller at all? Instead, I'd suggest that you start with an empty 'loading' screen and segue to the tab bar controller when the API call completes.

I'd also suggest that, rather than subclassing TabBarController for your API calls, you create a separate manager object to perform the update process, potentially as a singleton object (depending on other code considerations). You can then either call the shared instance from your view controllers, or inject the API call manager directly into each instance in prepare(for segue: sender:) on the presenting / parent view controller.



Related Topics



Leave a reply



Submit