首页 > 解决方案 > 在 tableviewcell 中隐藏带有 UITextField 的返回键的键盘

问题描述

我有一个带有嵌入式表格视图的视图,其中我有一个 UITextField 并且我想在按下完成按钮时隐藏键盘

我的故事板

我的代码如下:

我的表视图控制器:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "playerCell", for: indexPath) as? PlayerNameTableViewCell else {fatalError("Wrong type of cell")}

        // Configure the cell...
        cell.playerName.delegate = self

        return cell
}

我的手机:

class PlayerNameTableViewCell: UITableViewCell, UITextFieldDelegate {

    @IBOutlet weak var playerName: UITextField!

    @IBAction func enterButton(_ sender: UITextField) {
        sender.resignFirstResponder()

    }

}

但是当我按下完成按钮时没有任何反应。

标签: swiftxcode

解决方案


尝试将以下函数添加到实现 UITextFieldDelegate 的类中:

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    textField.resignFirstResponder()

    return true
}

推荐阅读