title: viewDidLoad called twice, using navigation controller
body: <p>My <code>ViewDidLoad</code> method on a <code>ViewController</code> is called twice, but only in a particular scenario. There are two view controllers which I need to present, one if user isn't logged in and the second if the user is logged in. I am using storyboard and have set a navigation controller as initial view controller in it. </p>
  
  <p>In my <code>AppDelegate</code> <code>didFinishLaunchingWithOptions</code> method I have populated <code>ViewControllers</code> array with the desired controller as below</p>
  
  <pre><code>let storyboard = UIStoryboard(name: "Main", bundle: nil)
  let navigationController = storyboard.instantiateViewController(withIdentifier: "navController") as! UINavigationController
  if UserDefaults.standard.object(forKey: USERID) != nil {
      viewController = storyboard.instantiateViewController(withIdentifier: "HomeVC_ID") as! HomeVC
  } 
  else {
      viewController = storyboard.instantiateViewController(withIdentifier: "LoginVC_ID") as! LoginVC
  }
  navigationController.viewControllers = [viewController] as! [UIViewController]
  self.window?.rootViewController = navigationController
  self.window?.makeKeyAndVisible()
  </code></pre>
  
  <p><code>ViewDidLoad</code> method in <code>HomeVC</code> is called twice, whereas it's called just once for <code>LoginVC</code>.</p>
  
  <p>I already tried searching through articles <a href="https://stackoverflow.com/questions/7079602/viewdidload-is-called-twice">viewDidLoad is called twice</a> and <a href="https://stackoverflow.com/questions/1081131/viewdidload-getting-called-twice-on-rootviewcontroller-at-launch">viewDidLoad getting called twice on rootViewController at launch</a> but couldn't corner the issue.</p>