【问题标题】:touchesBegan in static tableView not being called静态 tableView 中的 touchesBegan 未被调用
【发布时间】:2015-11-17 08:56:24
【问题描述】:

我在这里阅读了几篇关于同样问题的帖子,但主要是因为我在 IOS 开发方面很新,而且我使用 swift,所以无法理解它们。我什至找不到术语“子类化”的定义,也许它只是 obj-c?

无论如何,我有一个带有静态单元格的 tableview 控制器,以及一个单元格中的文本字段。当用户在编辑文本字段时点击不同的区域时,我需要关闭键盘。在这里阅读帖子后,我将 touchesBegan 内容更改如下:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        self.view.endEditing(true)
        self.nextResponder()?.touchesBegan(touches, withEvent: event)
        super.touchesBegan(touches, withEvent: event)
}

我仍然没有接触到定义为的 tableviewcontroller:

class addNew: UITableViewController, UITextFieldDelegate {

【问题讨论】:

    标签: swift uitableview tableview touchesbegan


    【解决方案1】:

    我在 Swift 4 上遇到了类似的问题,只是用 touchesBegan 结束编辑和隐藏键盘。一开始我找不到路。

    这两个步骤对我有用:

    1. 在表格视图中添加手势识别器:

      override func viewDidLoad() {
        super.viewDidLoad()
        tableView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hideKeyboard)))
      } 
      
    2. 中以两种方式结束编辑
      @objc func hideKeyboard() {
        view.endEditing(true)  
        //textField.resignFirstResponder()  /* This line also worked fine for me */
      }
      

    【讨论】:

      【解决方案2】:

      您可以在 tableview 顶部添加一个 tapGestureRecognizer。 将 tapGestureRecgonizer 连接到函数/方法。 在该方法中,检查文本字段是否是第一响应者。如果是,则要求文本字段辞职第一响应者。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-11
        相关资源
        最近更新 更多