title: Swift delegate not calling function in parent
body: <p>I am trying to get a child view controller, which is displayed in a view container at the bottom part of the main, parent View Controller. When I press the button in the child, "Button pressed from Basic VC" prints to the console, but "Warm button pressed from main VC" does not. I need the function that prints that second message to be called in the main View Controller. Here is the relevant BasicViewController code:</p>
  
  <pre><code>protocol BasicVCDelegate: class {
      func warmButton()
  }
  
  class BasicViewController: UIViewController {
      weak var BasicVCDelegate: BasicVCDelegate?
  
      @IBOutlet weak var warmButton: UIButton!
  
      @IBAction func warmButton(sender: AnyObject) {
          print("Button pressed from Basic VC")
          BasicVCDelegate?.warmButton()
      }
  }
  </code></pre>
  
  <p>And from ViewController.swift</p>
  
  <pre><code>class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, BasicVCDelegate {
      override func viewDidLoad() {
          pauseButton.isEnabled = false
  
          hideAll();
          self.basicContainer.isUserInteractionEnabled = true;
          self.basicContainer.isHidden = false;
  
          self.timerLabel.text = String("00:00:00");
  
          eventTable.dataSource = self
          eventTable.delegate = self
  
          loadEvents(event: "timer start")
          super.viewDidLoad()
      }
  
      // CREATE SEGUEs
      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
          if let nav = segue.destination as? UINavigationController, let classBasicVC = nav.topViewController as? BasicViewController {
              classBasicVC.BasicVCDelegate = self
          }
      }
  
      // FUNCTIONS FOR BASIC VIEW CONTROLLER TO REFERENCE
      func warmButton() {
          print("Warm button pressed from main VC")
      }
  }
  </code></pre>
  
  <p>EDIT:
  From everyone below, it seems that the following section of code in ViewController is causing the issue (the line within the if statement is never being called):</p>
  
  <pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let nav = segue.destination as? UINavigationController, let classBasicVC = nav.topViewController as? BasicViewController {
          classBasicVC.basicVCDelegate = self
      }
  }
  </code></pre>
  
  <p>This is probably due to my unfamiliarity with delegates and the prepareforsegue function. Need help figuring out what should follow that "if let"</p>
  
  <p>EDIT 2:
  Replaced the above code with the following as per some users' suggestion (still not reaching the print statement):</p>
  
  <pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let nav = segue.destination as? BasicViewController, let classBasicVC = nav.basicVCDelegate {
          print("got here!!")
      }
  }
  </code></pre>
  
  <p>FINAL EDIT:
  Thank you for your help! After some playing around, the following works (aside from a warning that changes to an error when I address it - I can live with a warning). </p>
  
  <pre><code>override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      if let nav = segue.destination as? BasicViewController, let classBasicVC = nav as? BasicViewController? {
          classBasicVC?.basicVCDelegate = self
      }
  }
  </code></pre>