swift - 已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing

标签 swift uitableview uitextfield

我有一个简单的 UITableViewController,它有 6 行,每行都有一个 UITextField。我希望能够单击每一行并将键盘应用于每一行。一切正常,除了出于某种原因我必须在每一行上单击两次以使响应者激活下一个 UITextField 。我输入了 UITextFieldDelegate 打印输出,它们的操作顺序似乎是错误的。每个 TextFields 都被标记为 0 - 5。当我选择第一个时,没有问题,键盘出现,我输入了一些东西。我的打印输出是:

textFieldShouldBeginEditing 标签:0 textFieldDidBeginEditing 标签:0

然后我选择下一行(没有点击键盘上的“完成”),打印输出是这样的:

textFieldShouldBeginEditing 标签:1 textFieldShouldEndEditing 标签:0 textFieldDidEndEditing 标签:0

为什么 textFieldDidBeginEditing 没有被调用?

这是我的代码以防有帮助:

class EstimateCell: UITableViewCell, UITextFieldDelegate {


  @IBOutlet weak var estimateField: UITextField!

  // callback to alert when user clicked "Done"
  var doneTextFieldInput: ((cell: EstimateCell, newText:String?) -> Void)?


  override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

   // self.selectionStyle = .None

    estimateField.delegate = self
    estimateField.clearsOnBeginEditing = true

    // add a done button to the numberpad
    addDoneButtonOnNumpad(estimateField)

    // add some padding to the right margin
    estimateField.layer.sublayerTransform = CATransform3DMakeTranslation(-20, 0, 0)
  }

  override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
  }

  /**
   Only allow 4 digits
   */
  func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    // only allow 4 digits max
    guard let text = textField.text else { return true }
    let newLength = text.characters.count + string.characters.count - range.length
    let isMax4digits = newLength <= 5
    return isMax4digits
  }

  func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
    print("textFieldShouldBeginEditing \(textField.tag)")

    return true
  }
  func textFieldDidBeginEditing(textField: UITextField) {
    textField.text = ""
    print("textFieldDidBeginEditing \(textField.tag)")
  }

  func textFieldShouldEndEditing(textField: UITextField) -> Bool {
    print("textFieldShouldEndEditing \(textField.tag)")
    return true
  }

  /**
   Called after the textfield resigns as the first responder
   */
  func textFieldDidEndEditing(textField: UITextField) {
    print("textFieldDidEndEditing \(textField.tag)")
    doneTextFieldInput?(cell: self, newText: textField.text)
   // textField.resignFirstResponder()
  }

  func textFieldShouldReturn(textField: UITextField) -> Bool {
    print("textFieldShouldReturn")
    textField.resignFirstResponder()
    return true
  }
  /**
   Adds a toolbar with a Done button as an input accessory view to the given textfield.  Calls
   resignFirstResponder when Done is clicked.
   */
  func addDoneButtonOnNumpad(textField: UITextField)
  {
    let keypadToolbar: UIToolbar = UIToolbar()

    // add a done button to the numberpad
    keypadToolbar.items=[
      UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: textField, action: #selector(UITextField.resignFirstResponder)),
      UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: self, action: nil)
    ]
    keypadToolbar.sizeToFit()
    // add a toolbar with a done button above the number pad
    textField.inputAccessoryView = keypadToolbar
  }
}

最佳答案

我想通了。问题是我在 textFieldDidEndEditing() 的回调中调用了 tableView.reloadData()。这导致设置我的 textfield.delegate = self 的代码再次触发,这是在调用 textFieldShouldBeginEditing() 之前完成的。希望有一天这可以帮助其他人:不要从 textFieldDidEndEditing() 中重新加载您的 tableview。

关于swift - 已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41458120/

相关文章:

ios - 通过 UIActivityViewController 共享 csv 文件

ios - 从侧面菜单中选择菜单项时,如何让 TabBarController 位于底部?

iphone - 在方向更改期间使用 drawRect 拉伸(stretch)

iphone - UITableView 滚动使应用程序崩溃

iPhone:显示键盘且方向更改时定位多个 UITextField

json - 错误 : Type 'Any' has no subscript members AlamoFire with Swift 4

ios - 节与其标题之间的 UITableView 空间

iphone - 无法触摸 UIScrollView 上的 UITextField

xcode - 使用 Swift 设置文本字段中的数字格式

ios - 如何使用 AutoLayout 快速平滑地对 UIImageView 进行动画处理?