I have text field in that i have to some functionality click on back space button in keyboard when the text field is empty ...shouldChangeCharactersInRange is calling when the textfield has some text.how call solve this problem.
-
Sorry, my earlier duplicate comment was to the wrong question. I meant this one: http://stackoverflow.com/questions/7757912/ios-how-do-i-get-notified-when-the-keyboards-back-button-is-pressed/7802788#7802788 hope the answer there helps you. – jrturton Oct 31 '11 at 08:22
-
@jrturton if i put empty space by explicitly over there place holder is removing but i need palce holder .one more problem i have secure text field in that case with entering any value its getting secure text – Narayana Rao Routhu Oct 31 '11 at 08:41
-
OK, what is supposed to happen when you press backspace on an empty field then? Perhaps you should consider having an input accessory view with it's own dedicated button, you can then connect this to any action you like. – jrturton Oct 31 '11 at 08:43
3 Answers
You should read the documentation of UITextField... By the way here it is from the documentation of UITextField
textFieldShouldClear: Asks the delegate if the text field’s current contents should be removed.
- (BOOL)textFieldShouldClear:(UITextField *)textField
Parameters
textField The text field containing the text.
Return Value
YES if the text field’s contents should be cleared; otherwise, NO.
Discussion
The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the
clearButtonMode
property of the text field.) This method is also called when editing begins and theclearsOnBeginEditing
property of the text field is set to YES.Implementation of this method by the delegate is optional. If it is not present, the text is cleared as if this method had returned YES.
Availability
Available in iOS 2.0 and later.
Declared In
UITextField.h

- 43,720
- 45
- 157
- 240

- 28,260
- 49
- 182
- 256
-
thank you for your reply.But i talking about backspace button keyboard – Narayana Rao Routhu Oct 31 '11 at 06:08
You can catch that key in
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
It is not written explicitly in the docs but if you press the backspace key then the last parameter string
will have a length of 0.
At least this worked for me when using UITextView objects.
Hope this helps :)

- 43,720
- 45
- 157
- 240
-
1this method calling only if text field have some text .if text field have no text then i click on back space that method not calling – Narayana Rao Routhu Oct 31 '11 at 06:35
-
good catch!. But I am afraid there is no way to catch keyboard events using public APIs in iOS. At least not that I know. Maybe this is the closest you can get? – nacho4d Oct 31 '11 at 08:43
If the text field is or becomes empty, put a space character in it. If you detect a backspace, and the dummy space character is the only thing in the text field, don't delete it (yet). When finally reading out the contents of the text field, remove the spurious leading space if you added one.

- 70,107
- 14
- 90
- 153
-
if i put space character place holder will not display but i need place holder when text field is empty..any help – Narayana Rao Routhu Oct 31 '11 at 06:42