0

I have a UITextField with date picker, and I want to use the default built in button to clear the text field, it doesn't seems to work.

I tried overriding

func textFieldShouldClear(_ textField: UITextField) -> Bool {
        searchBar.text = "" //my textfield is called searchBar and I tried to clear the text like that
        return true
    }

It seems that the method is called and printing the textfield says its empty but its not and in the view shows the last date I entered. Any ideas? Thanks in advance.

3 Answers3

0

You are searching this: textfield.clearButtonMode

Alastar
  • 1,284
  • 1
  • 8
  • 14
0

Have you tried to replace searchBar with textField (local variable) in the function like this?

func textFieldShouldClear(_ textField: UITextField) -> Bool {
        textField.text = "" // use local variable textfield here
        return true
    }
Richard R
  • 873
  • 6
  • 20
0

I found out that clear button is updating the text field and still takes the date from the date picker... Now I am trying to clear the value of the date picker maybe this will work.