Swift - press gesture to close UITableView keyboard

I have an identical question about this , but since I'm new to programming and only know quickly, I was wondering if anyone could give me its equivalent soon. Or point me to another question that I might have missed, it's quick.

Thanks!

UPDATE: here is the main point of my view controller after I cut off some of the fat in order to deal only with the relevant topic. To repeat the problem. Until I clicked on my "doneButton" to run the createClient () function and return to the client page to edit the newly created client, a click gesture will act to reject the keyboard.

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) } } 
+6
source share
6 answers
 override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard")) tapGesture.cancelsTouchesInView = true tableView.addGestureRecognizer(tapGesture) } func hideKeyboard() { tableView.endEditing(true) } 

Translating Objective-C code into Swift is not very difficult. You just need basic knowledge in both languages. If you are new to programming, I assume that you should first familiarize yourself with the basics.

+19
source

You can also do this from the storyboard:

enter image description here

+20
source

override func viewDidLoad () {super.viewDidLoad ()

  let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard")) tapGesture.cancelsTouchesInView = true self.view.addGestureRecognizer(tapGesture) } func hideKeyboard() { self.view.endEditing(true) } 

this will work fine compared to above, which doesn't work for me

+2
source

For some reason, @Isuru's answer is responding in my application (XCode 7.2.1), and fortunately this is just a minor change - just delete the Selector() call:

 override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: "hideKeyboard") tapGesture.cancelsTouchesInView = true tableView.addGestureRecognizer(tapGesture) } func hideKeyboard() { tableView.endEditing(true) } 
0
source

Swift 4:

In ViewDidLoad:

  override func viewDidLoad() { super.viewDidLoad() self.yourTableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))) } 

In the corresponding function:

 @objc func handleTap(_ sender: UITapGestureRecognizer) { if sender.state == .ended { // Do your thang here! self.view.endEditing(true) for textField in self.view.subviews where textField is UITextField { textField.resignFirstResponder() } } sender.cancelsTouchesInView = false } 
0
source

As @Ben already recommended, you can also do this programmatically. Just put this in your viewDidLoad ()

 tableView.keyboardDismissMode = .onDrag 
-1
source

Source: https://habr.com/ru/post/984105/


All Articles