我在每个表格单元格中都有几个UITextfields
。通过按“完成”按钮或触摸键盘外部,键盘应该被解除。
我的问题:textFieldShouldEndEditing
方法仅在我点击另一个文本字段时调用,键盘不会被解除。我想我已经实现了必要的部分(委托和协议方法)。
有谁知道我在这里失踪了....?
这是代码(相关部分):
class myVC: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {...
这是细胞设置......
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let tcell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "tbCell")
let textfield: UITextField = UITextField(frame:...
switch (indexPath.section) {
case 0: ...
case 1:
textfield.text = section2[indexPath.row]
tcell.addSubview(textfield)
textfield.delegate = self
return tcell
这个协议方法:
func textFieldShouldEndEditing(textField: UITextField) -> Bool {
textField.resignFirstResponder()
print("method is called")
return true
}
谢谢!
为什么不尝试使用textFieldShouldReturn UITextfield的委托。
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;}
我在Objective-C中给出了这个答案,因为我不使用Swift
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
当你滚动你的桌面视图,并且:
tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
当你在文本区域外触摸时。
Swift 4.0
tableView.keyboardDismissMode = .onDrag
要么
tableView.keyboardDismissMode = .interactive