Reputation: 697
I'm having problems with the below code storing the right characters from user: If the user enters "there" it stores "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
}
I also have these functions for my keyboard hiding:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
nameInputText.resignFirstResponder()
return (true)
}
Upvotes: 0
Views: 98
Reputation: 3538
Take note that shouldChangeCharactersIn
is being called when user start input a character and it only can appear in UITextField
when return true at the end of this method. (that is why your last input character not appear)
Thus, using shouldChangeCharactersIn
is not the right way track your changes. use UIControlEventEditingChanged
as suggested by @Rashwan
Upvotes: 1