已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing [英] textFieldShouldBeginEditing called, textFieldDidBeginEditing not called

查看:27
本文介绍了已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a simple UITableViewController with 6 rows, each with a UITextField. I want to be able to click on each row and have the keyboard apply to each. Everything works, except for some reason I have to click on each row twice to get the responder to make the next UITextField active. I put in UITextFieldDelegate printouts and the order of their actions seems wrong. Each of the TextFields has been tagged 0 - 5. When I select the first, there is no problem, the keyboard comes up, I enter something. My printout is:

textFieldShouldBeginEditing 标签:0textFieldDidBeginEditing 标签:0

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

Then I select the next row (without hitting Done in the keyboard) and the printout is this:

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

为什么没有调用 textFieldDidBeginEditing?

这是我的代码,以防万一:

Here is my code in case it helps:

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.

I figured it out. The problem is I was calling tableView.reloadData() in my callback inside textFieldDidEndEditing(). This caused the code that sets my textfield.delegate = self to fire again, which was being done before textFieldShouldBeginEditing() could be called. Hopefully this helps someone else out there one day: don't reload your tableview from inside textFieldDidEndEditing().

这篇关于已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆