title: Swift delegate to communicate between uiviewcontroller and uitableviewcontroller
body: <p>I've tried following several tutorials and stackoverflow questions to set up a protocol and delegate between two viewcontrollers to no avail. I'm fairly new to swift, so I'm unsure of what isn't working. I have a main uiviewcontroller and a uitableviewcontroller embedded within it. I need uitableviewcontroller to send information back to its parent. This is what I have in terms of code:</p>
  
  <pre><code>//Parent ViewController
  
  class parentViewController: UIViewController, childViewControllerDelegate { 
     func sendMessage(controller: childViewController, text: String) {
        print("Sent message: " + text)
     }
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>//Child ViewController
  
  protocol childViewControllerDelegate: class {
     func sendMessage(controller: childViewController, text: String) 
  }
  
  class childViewController: UITableViewController {
     weak var delegate: childViewControllerDelegate?
  
     override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if (indexPath.row == 0) {
           delegate?.sendMessage(self, text: "Hello Parent")
        }
     }
  }
  </code></pre>
  
  <p>Right now delegate?.sendMessage() is not being executed. I assume that delegate is nil and never set. Some suggestions I've seen says that the delegate must be set in the parent viewcontroller. I have attempted to change my parent viewcontroller to:</p>
  
  <pre><code>//Parent ViewController
  
  class parentViewController: UIViewController, childViewControllerDelegate { 
     weak var secondViewController: childViewController!
  
     override func viewDidLoad() {
        super.viewDidLoad()
  
        secondViewController.delegate = self
  
     }
  
     func sendMessage(controller: childViewController, text: String) {
        print("Sent message: " + text)
     }
  }
  </code></pre>
  
  <p>This code compiles but has a runtime error of fatal error: unexpectedly found nil while unwrapping an Optional value</p>