Ky -
Ky -

Reputation: 32143

How do I listen for the (x) button in a UITextField?

There's a tiny 🅧 button inside editable text UITextFields that clears the text. I want to listen for this to, say, reset the text "$0.00" instead of "", to close the dialog automatically, etc. How can I listen for this button to be pressed?

A white x inside a gray, filled circle on the right of a text field in an iOS table.

Upvotes: 2

Views: 297

Answers (2)

rob mayoff
rob mayoff

Reputation: 385970

In your text field's delegate, implement textFieldShouldClear:. From the documentation:

The text field calls this method in response to the user pressing the built-in clear button.

UPDATE

It sounds like you want to write this:

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    textField.text = @"";
    [self textFieldDidClear:textField];
    return NO;
}

- (void)textFieldDidClear {
    // To be overridden by subclasses.
}

Upvotes: 3

Wain
Wain

Reputation: 119031

Don't, instead set your cell as the delegate and when the text is updated to be an empty string you can deny the edit and directly set your text.

Upvotes: 0

Related Questions