gpt4 book ai didi

ios - 将来自 UITextField 的用户输入数据存储在 String 变量中

转载 作者:行者123 更新时间:2023-11-28 15:30:14 25 4
gpt4 key购买 nike

我在使用以下代码存储来自用户的正确字符时遇到问题:如果用户输入“there”,它会存储“ther”。

func textField(_ textField: UITextField,
shouldChangeCharactersIn range: NSRange,
replacementString string: String)
-> Bool
{
let text: String = nameInputText.text!
//print(text) //this prints My text
model.validateName(nametext: text)
print(text)
return true
}

我还有这些隐藏键盘的功能:

 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
nameInputText.resignFirstResponder()

return (true)
}

最佳答案

请注意,当用户开始输入字符时,shouldChangeCharactersIn 会被调用,并且只有在该方法结束时返回 true 时,它​​才会出现在 UITextField 中。 (这就是你最后输入的字符没有出现的原因)

因此,使用 shouldChangeCharactersIn 不是跟踪更改的正确方法。按照@Rashwan 的建议使用UIControlEventEditingChanged

关于ios - 将来自 UITextField 的用户输入数据存储在 String 变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44790237/

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