调用textFieldShouldBeginEditing,未调用textFieldDidBeginEditing

时间:2017-01-04 07:10:41

标签: swift uitableview uitextfield

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

textFieldShouldBeginEditing tag: 0 textFieldDidBeginEditing tag: 0

然后我选择下一行(没有在键盘上点击Done),打印输出为:

textFieldShouldBeginEditing tag: 1 textFieldShouldEndEditing tag: 0 textFieldDidEndEditing tag: 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
  }
}

1 个答案:

答案 0 :(得分:3)

我明白了。问题是我在tableView.reloadData()内的回调中调用textFieldDidEndEditing()。这导致了我的textfield.delegate = self再次触发的代码,这可以在调用textFieldShouldBeginEditing()之前完成。希望有一天能帮助其他人:不要从textFieldDidEndEditing()内部重新加载你的桌面视图。