custom action for clear button of UITextfield in swift

Viewed 5354

I have a UITextfield which has clear button enabled only while editing and it as shown in image attached. enter image description here

What i need is when i tap on clear button of UITextfield it should do few custom activity along with clearing the UItextfield. As of now it only clears the UITextField only. It is not specific to clear/ dismiss keyboard.

Thanks

1 Answers

You can use this UITextFieldDelegate method. Don't forget to set delegate for the textfields. UITextFieldDelegate

func textFieldShouldClear(_ textField: UITextField) -> Bool {
    print("text cleared")
    //do few custom activities here
    return true
  }
Related