title: Swift - Tap gesture to dismiss keyboard UITableView
body: <p>I have an identical question to <a href="https://stackoverflow.com/questions/2321038/dismiss-keyboard-by-touching-background-of-uitableview">this one</a> , but since I'm new to programming and only really know swift I was wondering if someone could give me its equivalent in swift. Or point me to another question that I may have missed that is in swift. </p>
  
  <p>Thanks!</p>
  
  <p>UPDATE: here's the basic jist of my view controller after I've cut some of the fat away to deal with only the relevant topic. To restate the problem. Not until I have clicked my 'doneButton' to run the createClient() function and navigate back to the client page to edit the freshly created client will the the tap gesture work to dismiss the keyboard.</p>
  
  <pre><code>import UIKit
  import CoreData
  import Foundation
  
  class NewClientTableViewController: UITableViewController, UINavigationControllerDelegate, UITextFieldDelegate {
  
  let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
  
  @IBOutlet weak var nameTxt: UITextField!
  @IBOutlet weak var ageTxt: UITextField!
  @IBOutlet weak var phoneTxt: UITextField!
  @IBOutlet weak var emailTxt: UITextField!
  @IBOutlet weak var heightTxt: UITextField!
  @IBOutlet weak var notesTxt: UITextView!
  
  var client: Client? = nil
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      if client != nil {
  
          nameTxt.text = client?.name
          ageTxt.text = client?.age
          heightTxt.text = client?.height
          phoneTxt.text = client?.phone
          emailTxt.text = client?.email
          notesTxt.text = client?.notes
  
          self.title = client?.name
          phoneTxt.delegate = self
  
          let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
          tapGesture.cancelsTouchesInView = true
  
          tableView.addGestureRecognizer(tapGesture)
  
      }
  }
  
  func hideKeyboard() {
      tableView.endEditing(true)
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated.
  }
  
  
  @IBAction func doneButton(sender: AnyObject) {
      if client != nil {
          editClient()
      } else {
          createClient()
      }
      dismissViewController()
  
  }
  
  func editClient() {
  
      client?.name = nameTxt.text
      client?.age = ageTxt.text
      client?.height = heightTxt.text
      client?.phone = phoneTxt.text
      client?.email = emailTxt.text
      client?.notes = notesTxt.text
      client?.clientImage = UIImageJPEGRepresentation(contactImage.image, 1)
  
      managedObjectContext?.save(nil)
  }
  
  func createClient() {
  
      let entityDescription = NSEntityDescription.entityForName("Client", inManagedObjectContext: managedObjectContext!)
      let client = Client(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)
  
      if nameTxt.text == "" {
          client.name = "Untitled Client"
      } else {
          client.name = nameTxt.text
      }
  
      client.age = ageTxt.text
      client.height = heightTxt.text
      client.phone = phoneTxt.text
      client.email = emailTxt.text
      client.notes = notesTxt.text
  
      client.clientImage = UIImageJPEGRepresentation(contactImage.image, 1)
  
      managedObjectContext?.save(nil)
  
  }
  
  func dismissViewController() {
      navigationController?.popToRootViewControllerAnimated(true)
      }
  }
  </code></pre>