如何点击隐藏键盘,然后再次点击以选择Swift中的单元格?

时间:2018-04-11 05:54:56

标签: swift uigesturerecognizer

我有以下扩展名,当在视图中的任何位置注册点击时,它会隐藏键盘。

    //Extension to hide the keyboard when tap anywhere
extension UIViewController {
    func hideKeyboardWhenTappedAround()  {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)

    }

    @objc func dismissKeyboard() {
        view.endEditing(true)
    }
}

这在我的ViewController的viewDidLoad()中使用,它也是TableView控制器的委托/数据源。

 self.hideKeyboardWhenTappedAround()

键盘的解除效果非常好,虽然我所遵循的行为是键盘首先在视图/ tableview上的任何位置点击,然后用户再次点击以从中选择一行在tableview中搜索结果。

目前,任何地方的点按不仅可以解除键盘,还可以选择用户点按的单元格。

3 个答案:

答案 0 :(得分:0)

在didselectrow方法中添加通知观察者和检查键盘。

var isKeyBoard = false

override func viewDidLoad() {

    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillDisappear(_:)), name: Notification.Name.UIKeyboardWillHide, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillAppear(_:)), name: Notification.Name.UIKeyboardWillShow, object: nil)
}

@objc func keyboardWillAppear(_ notification: NSNotification) {
    if let userInfo = notification.userInfo, 
       isKeyBoard = true
    }
}

@objc func keyboardWillDisappear(_ notification: NSNotification) {
   isKeyBoard = false
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       if isKeyBoard {
         self.view.endEditing(true)
         return

       }

    }

答案 1 :(得分:0)

只需正常检查即可初始化为

键盘已打开这意味着TextField充当第一响应者

现在你只需要在这里检查didSelect方法我在我的集​​合中显示你看到的DidSelect方法与你在Tableview中所做的相同DidSelect

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        /// Check do TF is First Responder 
        /// If yes Just Hide the Keyboard for first time
        if propertySearchTF.isFirstResponder {
            self.view.endEditing(true)
            return
        }
        /// if TF is not First Responder 
        /// Execution will start from here
        /// DidSelect Code
    }

在TableView中测试:

TF Outlet

@IBOutlet weak var myTF: UITextField!{
        didSet{
            myTF.becomeFirstResponder()
        }
    }

TableView方法

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if self.myTF.isFirstResponder {
                self.view.endEditing(true)
                return
            }
            print(selectedKey)
            selectedimage = images[indexPath.row] as UIImage
            performSegue(withIdentifier: "segue", sender: self)
        }

答案 2 :(得分:0)

您必须使用cancelsTouchesInView = true

在tableView上添加手势
extension UITableView {

    func hideKeyboardWhenTappedAround()  {
        let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
        tap.cancelsTouchesInView = true
        self.addGestureRecognizer(tap)
    }

    @objc func dismissKeyboard() {
        self.endEditing(true)
    }
}

来自viewDidLoad() tableView.hideKeyboardWhenTappedAround()

的来电