26

I have a tabbar controller as the root view controller. I would like to pre-load the views of tab [1,2,3] (tab 0 loads as the first tab automatically).

I essentially would like the code in viewdidload to run before the user taps on the tab.

Thanks

Laurent May
  • 265
  • 1
  • 3
  • 5

8 Answers8

34

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)];
Nik
  • 9,063
  • 7
  • 66
  • 81
Joel Kravets
  • 2,473
  • 19
  • 16
16

In iOS 8, I created a subclass of UITabViewController named TSMainBarViewController. In the function viewDidLoad of TSMainBarViewController, I just added this code:

for(UINavigationController * viewController in self.viewControllers){
    [[viewController.viewControllers firstObject] view];
}

Then all the viewControllers (root viewController of UINavigationController) will load . All my viewControllers are created in the StoryBoard. Do not implement the function loadView in the UIViewController.


Swift 3 code from EligyD:

for viewController in self.viewControllers! {
    _ = viewController.view
}
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
guozqzzu
  • 839
  • 10
  • 12
10

To handle tab controllers which might or might not have navigation controllers:

viewControllers?.forEach {
    if let navController = $0 as? UINavigationController {
        navController.topViewController?.view
    } else {
        $0.view.description
    }
}
Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87
2

If your all view controllers under the tab bar controller have a navigation bar embedded in them:

Swift 3

for navViewController in self.viewControllers! {
    _ = navViewController.childViewControllers[0].view
}

User Panda's solution didn't work for me.

CocoaNuts
  • 179
  • 1
  • 13
1

if you have UINavigationController before your views

for viewController in self.tabBarController!.viewControllers! {
    var aView = viewController.topViewController as UIViewController
    aView.view.description
}
Amr Mohamed
  • 2,290
  • 4
  • 20
  • 39
0

Swift 3:

In viewDidLoad() of your UITabBarController

for viewController in self.viewControllers! {
    _ = viewController.view
}
Borzh
  • 5,069
  • 2
  • 48
  • 64
ElegyD
  • 4,393
  • 3
  • 21
  • 37
0

Swift 5:

viewControllers?.forEach {
  if let navController = $0 as? UINavigationController {
     let _ = navController.topViewController?.view
  } else {
      let _ = $0.view.description
    }
 }
-1

Swift equivalent...

for viewController in self.tabBarController.viewControllers!
{
    viewController.view;
}
TWilly
  • 4,863
  • 3
  • 43
  • 73