Quentamia
Quentamia

Reputation: 3292

(iOS) How do I get notified when the keyboard's back button is pressed?

I'm trying to determine when the keyboard's back button is pressed. When there is text in the text field, this is easy. I simply use the delegate method - (BOOL)textField:(UITextField *)aTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string. The problem is when the text field is empty and the back button is pressed, this method isn't called.

Does anyone know how to get notified even when the text field is empty?

Upvotes: 0

Views: 3447

Answers (3)

UPT
UPT

Reputation: 1480

In the delegate method:

(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)string

keep the earlier string and compare it with the string, if count of string is 1 less than the previous modified string it means delete is pressed. Otherwise store string into earlier string. hope this will help you.

Upvotes: -1

jrturton
jrturton

Reputation: 119292

Could you default in a single space to the empty field when you begin editing (and the field is empty) so that the backspace becomes a normal event? You could remove the space when leaving the field and/or the first normal character is typed. Bit of a hack but I don't think you'll get to it any other way.

Upvotes: 3

johntraver
johntraver

Reputation: 3612

Check out this answer..

Receive iPhone keyboard events

In your keyPressed function, check to see if the textfield value has any content;

Upvotes: -1

Related Questions