Can someone explain about when to use and how difference of them ?.
Asked
Active
Viewed 333 times
0
-
1What is `textFieldDidChange`? There's no such thing in `UITextFieldDelegate`. There's only `textFieldDidChangeSelection`. Did you confuse it with something else? For `shouldChangeCharactersIn`, see [my explanation here](https://stackoverflow.com/questions/61433592/how-does-replacingcharacters-work-in-a-delegate/64154028#64154028) in the context of limiting the number of characters in the textfield. – Sweeper Apr 21 '21 at 03:41
1 Answers
1
In Simple words textDidChange
occurs when any text is typed in Textfield and you want to get text typed on TextField
to check for validation or anything...
func textDidChange(_ textField: UITextField) {
guard let email = emailTextField.text, !email.isEmpty, email.isValidEmail() else {
//Write your code here...
return }
guard let password = passwordTextField.text, !password.isEmpty else {
//Write your code here..
return }
//Write your success code.
}
And shouldChangeCharactersIn
occurs event before any typed key is about to print in Textfield. So that you can varify that key and allow or restrict that key.
Eg: If user enter space in Password Textfield you can restrict that key and space will never is printed in Textfield.
In below code I am restricting space(" ") key in My Textfields.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if range.location == 0 && string == " " {
return false
}
return true
}

Kudos
- 1,224
- 9
- 21