title: UIViewController call function in container view viewcontroller
body: <p>Using Swift 3. This question has been around and there are quite a few examples. Unfortunately none of them have worked for me. I have a uiviewcontroller that has a container view. This container view has a segue to another view controller. Everything loads up correctly and displays correctly the first time. The problem that I am facing is that I have a button on the main uiviewcontroller which by tapping it, it will refresh the container view. This is where the problem is. The outlets in the child controller are nil.</p>
  
  <p>This is how I am trying to all the function in the child controller.</p>
  
  <pre><code>let wcvc = self.storyboard!.instantiateViewController(withIdentifier: "WeatherCurrentViewController") as! WeatherCurrentViewController
      wcvc.callJson()
  </code></pre>
  
  <p>In the child controller the label is like this</p>
  
  <pre><code> @IBOutlet var currentweather: UILabel!
  </code></pre>
  
  <p>But in the function that is called I get an error of unexpectedly found nil while unwrapping an Optional value when I try to do</p>
  
  <pre><code> print(currentweather)
  </code></pre>
  
  <p>Anyone have an idea of what I can do to call a function in a view controller that is loaded by segue in a container view and still have the outlets be valid?  Please let me know if more info is needed.</p>
  
  <p>Found the solution. I needed to use segues identifiers and not the storyboard. Doing this</p>
  
  <pre><code> private var currentEmbeddedViewController: WeatherCurrentViewController!     
   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let cvc = segue.destination as? WeatherCurrentViewController, segue.identifier == "CurrentWeather" {
          self.currentEmbeddedViewController = cvc
      }
  } 
  </code></pre>
  
  <p>And calling the function in the controller like so</p>
  
  <pre><code>self.currentEmbeddedViewController.callJson()
  </code></pre>
  
  <p>Was the solution.  This <a href="https://stackoverflow.com/questions/40727498/uiviewcontroller-call-function-in-container-view-viewcontroller">question</a> offered the solution. Sorry to waste everyones time. Hopefully, this will help someone else.</p>