Reputation: 301
Can someone explain about when to use and how difference of them ?.
Upvotes: 1
Views: 603
Reputation: 1490
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
}
Upvotes: 2