swift - 在 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()
}
}
但是当我按下完成按钮时没有任何反应。
解决方案
尝试将以下函数添加到实现 UITextFieldDelegate 的类中:
func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
textField.resignFirstResponder()
return true
}
推荐阅读
- jquery - 为什么当光标在顶部时,悬停时出现的div开始闪烁?
- python - 你如何让用户输入“DONE”结束程序并说出在 python 中有多少问题是对的和错的?
- reactjs - 使用 .map 并收到警告:道具类型失败:道具 `to` 在 `Link` 中标记为必需,但其值为 `undefined`
- c# - 如何检查输入的数据是否在表格中?
- excel - 如何删除超过一个月的工作表?
- python - 合并重复值并汇总字典列表中的总数
- python - 如何基于单元格中的多行跨每一行创建多个熊猫数据框列?
- python - 如何在直方图中添加图例?
- css - 如何像表格一样弯曲列宽
- git - 无法将本地更改推送到远程 bitbucket