Swift - 点击手势关闭键盘 UITableView
Swift - Tap gesture to dismiss keyboard UITableView
我有一个与 this one 相同的问题,但由于我是编程新手并且只真正知道 swift 我想知道是否有人可以给我它在 swift 中的等价物.或者指出 swift 中我可能遗漏的另一个问题。
谢谢!
更新:这是我的视图控制器的基本内容,在我削减了一些内容以仅处理相关主题之后。重述问题。直到我单击 'doneButton' 到 运行 createClient() 函数并导航回客户端页面以编辑新创建的客户端时,点击手势才会关闭键盘。
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)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
将 Objective-C 代码翻译成 Swift 并不难。您只需要两种语言的基本知识。如果您是编程新手,我想您应该首先熟悉基础知识。
您也可以从情节提要中进行:
出于某种原因,@Isuru 的回答在我的应用程序 (XCode 7.2.1) 中崩溃了,幸运的是这只是一个小改动 - 只需删除 Selector()
调用:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "hideKeyboard")
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
覆盖 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)
}
与上面对我不起作用的方法相比,这会很好用
正如@Ben 已经推荐的那样,您也可以通过编程方式执行此操作。
只需将其放入您的 viewDidLoad()
tableView.keyboardDismissMode = .onDrag
Swift 4:
在 ViewDidLoad 上:
override func viewDidLoad() {
super.viewDidLoad()
self.yourTableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))))
}
对应函数:
@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
}
接受的答案在 XCode 13.1 上对我不起作用,所以我稍微更改了代码:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
@objc func hideKeyboard() {
tableView.endEditing(true)
}
我有一个与 this one 相同的问题,但由于我是编程新手并且只真正知道 swift 我想知道是否有人可以给我它在 swift 中的等价物.或者指出 swift 中我可能遗漏的另一个问题。
谢谢!
更新:这是我的视图控制器的基本内容,在我削减了一些内容以仅处理相关主题之后。重述问题。直到我单击 'doneButton' 到 运行 createClient() 函数并导航回客户端页面以编辑新创建的客户端时,点击手势才会关闭键盘。
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)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
将 Objective-C 代码翻译成 Swift 并不难。您只需要两种语言的基本知识。如果您是编程新手,我想您应该首先熟悉基础知识。
您也可以从情节提要中进行:
出于某种原因,@Isuru 的回答在我的应用程序 (XCode 7.2.1) 中崩溃了,幸运的是这只是一个小改动 - 只需删除 Selector()
调用:
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "hideKeyboard")
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
func hideKeyboard() {
tableView.endEditing(true)
}
覆盖 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)
}
与上面对我不起作用的方法相比,这会很好用
正如@Ben 已经推荐的那样,您也可以通过编程方式执行此操作。 只需将其放入您的 viewDidLoad()
tableView.keyboardDismissMode = .onDrag
Swift 4:
在 ViewDidLoad 上:
override func viewDidLoad() {
super.viewDidLoad()
self.yourTableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:))))
}
对应函数:
@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
}
接受的答案在 XCode 13.1 上对我不起作用,所以我稍微更改了代码:
override func viewDidLoad() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.hideKeyboard))
tapGesture.cancelsTouchesInView = true
tableView.addGestureRecognizer(tapGesture)
}
@objc func hideKeyboard() {
tableView.endEditing(true)
}