gpt4 book ai didi

swift - 已调用 textFieldShouldBeginEditing,未调用 textFieldDidBeginEditing

转载 作者:行者123 更新时间:2023-11-28 10:59:27 25 4
gpt4 key购买 nike

我有一个简单的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com