1

I am working on making a tabbed app. It has a TabBarController and 4 ViewControllers attached to it.

By default, at launch only FirstViewController is loaded. I would like to load both FirstViewController and SecondViewController at start before switching to the second view via tab menu.

What i tried so far is that I created custom MyTabBarController class and tried to use

var sv = SecondViewController()
sv.loadView()

in ViewDidLoad(), but it caused a fatal error during loading, because (my guess) mapView element from storyboard was not loaded.

What is the correct way to simultaneously load the two viewControllers which use storyboard elements? All my other tries have not been successful so far.

Alice A.
  • 85
  • 1
  • 7
  • Are you creating your UITabBarController programmatically? If so, you can programmatically create each of your view controllers and assign them to the `viewControllers` property of your tab bar controller. But this isn't a typical design pattern. May I ask why you're trying to do this? Is it for performance reasons (to pre-load the second controller to avoid a delay)? – Anna Dickinson Feb 16 '17 at 17:45
  • Hmm, if i'm getting your question correctly - I created TabBarController it in storyboard, then created `MyTabBarController: UITabBarController` class and assigned it to storyboard element. I did only as a possible means to load 2 View Controllers at once, since i thought it's required. The reason why i'm need them loaded at the same time is exactly about avoiding a delay – Alice A. Feb 16 '17 at 17:52

1 Answers1

1

Add in your main view controller

var secondViewController:UIViewController!

And in your viewDidLoad:

secondViewController: UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "yourIdentifier") as! SecondViewController

That's it. When you want to present it, use:

self.present(secondViewController, animated: true, completion: nil)
J Manuel
  • 3,010
  • 22
  • 39
  • No problem @AliceA.! Mark it as correct answer so other people that are facing the same issue can fix it. – J Manuel Feb 16 '17 at 18:04