0

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)
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Kevin
  • 667
  • 2
  • 7
  • 18

1 Answers1

1

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

xmhafiz
  • 3,482
  • 1
  • 18
  • 26