【问题标题】:textFieldShouldBeginEditing called, textFieldDidBeginEditing not called调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing
【发布时间】:2017-01-04 07:10:40
【问题描述】:

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

textFieldShouldBeginEditing tag: 0 textFieldDidBeginEditing tag: 0

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

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
  }
}

【问题讨论】:

  • 尝试从 awakeFromNib() 方法中删除以下代码 estimateField.clearsOnBeginEditing = true
  • 不幸的是,没有这样做。感谢您的尝试!我唯一的另一个想法是更改为 UIViewController 并将 UITableView 放入其中并尝试这样。我一开始没有这样做的原因是在选择较低的文本字段之一时获得自动滚动功能。
  • 感谢您的快速审核,祝您好运@Coder1224

标签: swift uitableview uitextfield


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2012-10-31
    • 1970-01-01
    • 2014-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多