【发布时间】:2017-05-30 00:07:56
【问题描述】:
我是 Swift 新手,当 textField 位于自定义 tableview 单元格上时,我一直在努力隐藏键盘。我认为问题源于 TableViewCell 类中的 textField 引用,但我不能确定。我什么都试过了,有点迷茫。
我的代码包括:
TableViewCell:
import UIKit
class TableViewCell: UITableViewCell, UITextFieldDelegate
{
@IBOutlet weak var userText: UITextField!
@IBAction func answers(_ sender: UITextField)
{
}
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.delegate = self
textField.resignFirstResponder()
return true
}
}
和 TableViewController:
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate
{
var textfield = TableViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
let myText = textfield.userText
textField.resignFirstResponder()
myText?.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
}
我尝试从两个类中运行 textFieldShouldReturn 函数,但我无法让它工作。
【问题讨论】:
-
你试过 textField.resignFirstResponder 了吗?
标签: ios swift uitableview uitextfield