Swift - 点击手势以关闭键盘UITableView

时间:2015-03-22 14:42:56

标签: uitableview swift uitapgesturerecognizer

我对this one提出了一个相同的问题,但由于我是编程新手并且只是真的知道很快,我想知道是否有人可以在swift中给我相同的东西。或者指出我可能已经错过的另一个问题是快速的。

谢谢!

更新:这是我视线控制器的基本要点,因为我已经削减了一些脂肪,只处理相关主题。重申问题。直到我点击了我的完成按钮'要运行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)
    }
}

6 个答案:

答案 0 :(得分:27)

您也可以从Storyboard中执行此操作:

enter image description here

答案 1 :(得分:24)

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并不是很难。您只需要两种语言的基本知识。如果您不熟悉编程,我想您应该首先熟悉基础知识。

答案 2 :(得分:2)

覆盖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)
}

与上面对我不起作用相比,这样可以正常工作

答案 3 :(得分:1)

出于某种原因,@ Isuru的答案在我的应用程序(XCode 7.2.1)中崩溃了,谢天谢地,这只是一个小小的改动 - 只需删除SQL Server translate电话:

Selector()

答案 4 :(得分:1)

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
}

答案 5 :(得分:0)

正如@Ben已推荐的那样,您也可以通过编程方式执行此操作。 把它放在你的viewDidLoad()

tableView.keyboardDismissMode = .onDrag