views:

1289

answers:

5

Is there any way to detect when the backspace/delete key is pressed in the iPhone keyboard on a UITextField that is empty? I want to know when backspace is pressed only if the UITextField is empty.


Based on the suggestion from @Alex Reynolds in a comment, I've added the following code while creating my text field:

[[NSNotificationCenter defaultCenter] addObserver:self
          selector:@selector(handleTextFieldChanged:)
              name:UITextFieldTextDidChangeNotification
            object:searchTextField];

This notification is received (handleTextFieldChanged function is called), but still not when I press the backspace key in an empty field. Any ideas?


There seems to be some confusion around this question. I want to receive a notification when the backspace key is pressed. That's it. But the solution must also work when the UITextField is already empty.

A: 

Look into the UITextFieldTextDidBeginEditingNotification and UITextFieldTextDidEndEditingNotification notifications. If you begin and end editing, and the text doesn't change, what button was likely pressed?

Alex Reynolds
"What button was likely pressed?" This doesn't solve the issue is what I'm trying to say. This notification gets called when I exit the field. Let me restate my question: I want to know when the backspace button is pressed in an empty UITextField. I don't want to know after the field has resignedFirstResponder.
marcc
+5  A: 

This may be a long shot but it could work. Try setting the text field's text to a zero width space character \u200B. When backspace is pressed on a text field that appears empty, it will actually delete your space. Then you can just reinsert the space.

May not work if the user manages to move the caret to the left of the space.

Andrew
Nice. I agree that it's pretty hacky, but I was considering something like this as a last resort. Thanks for the suggestion.
marcc
@Andrew, this is the approach I decided to take. It took a bit of code, but it's certainly effective. Thanks for the help instead of trying to tell me that I'm doing something wrong.
marcc
A: 

Why. Someone could probably offer an alternative if you tell us what you want to do when Backspace is pressed on an empty text field.

Kendall Helmstetter Gelner
I want to update another field. It's for a custom control. It's an unusual use case, but I can assure you that Apple is doing the same thing in their SMS app.
marcc
Where? When I use the SMS app nothing happens if I use backspace in an empty message field, either on the To line in a new message or the text entry field for the message itself (which I'm pretty sure is a UITextView). If we know the exact behavior you are trying to replicate, it may lead to a good clue as to how they are making it work.
Kendall Helmstetter Gelner
Address an SMS message to an existing contact. Now put the cursor in the empty text field to the right of this contact. Press backspace. Something happens. I know this is a very custom control and I'm trying to do something somewhat similar, but creating my own control.
marcc
Aha, now I see what you mean... the interesting thing to me, is that I'm not sure that text field is empty. It's almost as if they have painted a bubble around text in the text field, so when you hit "back" it's really just deleting a space and changing the overlay... Another alternative might be to try placing a button over the keyboard "backspace" button. to intercept the press until something is typed in the field.
Kendall Helmstetter Gelner
A: 

Try the delegate

  • (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

Then check if the range.length == 1... Which seems to be the case when backspace is hit...

Niklas Alvaeus
A: 

Niklas Alvaeus's answer helped me out with a similar issue

I was limiting entry to a specific character set... but it was ignoring backspaces... so I had it check range.length == 1 before triming the string... it it was true i just return the string and don't trim it. See below

  • (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];

    if (range.length == 1){ return string; }else{ return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0); }

}

Ben Call