title: How to make the cancel button in alert cancel the action?
body: <p>I made an alert as you can see, but the app adds the item, no matter if I'm clicking the "NO" button og the "Yes, I'm sure" button when the alert pops up. </p>
  
  <p>My goal is to make the "NO" action, cancel the action so the input won't be added anyway. Can you tell me how to?</p>
  
  <pre><code>import UIKit
  
  class SecondViewController: UIViewController, UITextFieldDelegate {
      @IBOutlet weak var input: UITextField!
  
      @IBAction func addItem(_ sender: Any)
      {
          createAlert(title: "That's a good grail!", message: "Are you sure you want to add this grail?")
  
          if (input.text != "")
          {
              list.append(input.text!)
              input.text = ""
          }
      }
  
      override func viewDidLoad()
      {
          super.viewDidLoad()
  
          self.input.delegate = self
      }
  
      //HIDE KEYBOARD:
      override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
          self.view.endEditing(true)
      }
  
      //PRESSES RETURN KEY:
      func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
          input.resignFirstResponder()
          return true
      }
  
      func createAlert (title:String, message:String)
      {
          let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
  
          //CREATING OK BUTTON
  
          let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { (action:UIAlertAction!) in
  
              // Code in this block will trigger when OK button tapped.
              print("Ok button tapped");
  
          }
          alertController.addAction(OKAction)
  
          // Create Cancel button
          let cancelAction = UIAlertAction(title: "No!", style: .cancel) { (action:UIAlertAction!) in
              print("Cancel button tapped");
          }
          alertController.addAction(cancelAction)
  
          // Present Dialog message
          self.present(alertController, animated: true, completion:nil)
      }
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>The code looks like this now, thanks:</p>
  
  <p>import UIKit</p>
  
  <p>class SecondViewController: UIViewController, UITextFieldDelegate {</p>
  
  <pre><code>@IBOutlet weak var input: UITextField!
  
  @IBAction func addItem(_ sender: Any)
  {
      createAlert(title: "That's a good grail!", message: "Are you sure you want to add this grail?")
  
  }
  
  
  
  override func viewDidLoad()
  {
      super.viewDidLoad()
  
      self.input.delegate = self
  }
  
  
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  //HIDE KEYBOARD:
  override func touchesBegan(_ touches: Set&lt;UITouch&gt;, with event: UIEvent?) {
      self.view.endEditing(true)
  }
  
  //PRESSES RETURN KEY:
  func textFieldShouldReturn(_ textField: UITextField) -&gt; Bool {
      input.resignFirstResponder()
      return true
  }
  
  
  func createAlert (title:String, message:String)
  {
      let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
  
      //CREATING OK BUTTON
  
      let OKAction = UIAlertAction(title: "Yes, I'm sure!", style: .default) { (action:UIAlertAction!) in
  
          // Code in this block will trigger when OK button tapped.
          print("Ok button tapped");
          if (self.self.input.text != "")
          {
              list.append(self.input.text!)
              self.input.text = ""
          }
  
      }
      alertController.addAction(OKAction)
  
      // Create Cancel button
      let cancelAction = UIAlertAction(title: "No!", style: .cancel) { (action:UIAlertAction!) in
          print("Cancel button tapped");
      }
      alertController.addAction(cancelAction)
  
      // Present Dialog message
      self.present(alertController, animated: true, completion:nil)
  }
  </code></pre>
  
  <p>}</p>