作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在使用以下代码存储来自用户的正确字符时遇到问题:如果用户输入“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/
我是一名优秀的程序员,十分优秀!