title: How to use callback to send back data to previous ViewController
body: <p>I have 2 ViewControllers like this:</p>
  
  <p><a href="https://i.stack.imgur.com/fy9sW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fy9sW.png" alt="enter image description here"></a></p>
  
  <p>I want when I press button in ViewController will present to ViewController2. Then when I input the text in TextField and press Back Button in ViewController2, will send that text to button in ViewController1.</p>
  
  <p>I can solve this with delegate or using reference the class ViewController in ViewController2. (You can see with my comment code)</p>
  
  <p>But now I want to use CallBack to send back data. </p>
  
  <p>How to do this:</p>
  
  <p>Here is ViewController:</p>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
  
      @IBOutlet var button: UIButton!
      var myString = ""
  
      override func viewDidLoad() {
          super.viewDidLoad()
      }
  
      override func viewWillAppear(animated: Bool) {
          button.setTitle(myString, forState: .Normal)
      }
      @IBAction func goToViewController2(sender: AnyObject) {
          let vc2 = storyboard?.instantiateViewControllerWithIdentifier("ViewController2") as! ViewController2
  //        vc2.vc1 = self
          presentViewController(vc2, animated: true, completion: nil)
      }
  }
  </code></pre>
  
  <p>And ViewController2:</p>
  
  <pre><code>import UIKit
  
  class ViewController2: UIViewController, UITextFieldDelegate {
  
      @IBOutlet var textField: UITextField!
  //    var vc1: ViewController?
      var callback: ((String) -&gt; Void)?
  
      override func viewDidLoad() {
          super.viewDidLoad()
          textField.delegate = self
      }
  
      func textFieldDidEndEditing(textField: UITextField) {
          callback!(textField.text!)
      }
  
      @IBAction func backToViewController(sender: AnyObject) {
  //        vc1?.myString = textField.text!
          dismissViewControllerAnimated(true, completion: nil)
      }
  }
  </code></pre>
  
  <p>And I don't want to use with completion block in dismiss function, I just want to use my own callback method.</p>