0

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?

Quentamia
  • 3,244
  • 3
  • 33
  • 42

3 Answers3

3

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
-1

Check out this answer..

Receive iPhone keyboard events

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

Community
  • 1
  • 1
johntraver
  • 3,612
  • 18
  • 17
  • Thanks. I tried this. If the textfield is already empty, the event doesn't get fired :( I'll keep searching. – Quentamia Oct 17 '11 at 21:38
-1

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.

UPT
  • 1,490
  • 9
  • 25
  • Thanks. Same problem with the above answer. I need to know when the backspace button is pressed even if the textfield is empty, and that delegate method only fires off if the text changes. Empty to empty doesn't count. – Quentamia Oct 18 '11 at 15:23
  • Some key is pressed and you don't get any text, it means back space / delete is pressed...(just check if delegate method is called in empty text field) – UPT Oct 18 '11 at 16:56
  • That was the problem. The delegate method isn't called if the text field is already empty. I found a workaround by always keeping an extra space in the text field. – Quentamia Oct 20 '11 at 23:54
  • please read question properly and then answer it... as it is already mentioned that "problem comes when textfield is empty" – Mehul Thakkar Feb 02 '15 at 13:10