How do I identify when user press keys in keyboard? What is delegate method for it?
Asked
Active
Viewed 88 times
2 Answers
1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSLog(@"%@",[NSString stringWithFormat:@"%d", [string length]]);
if (string.length==0)
{
NSLog(@"This is BackSpace");
}
return YES;
}
-
how to identify when user press backspace button?. – user2003416 Jan 26 '13 at 10:08
1
set textFields text to a zero width space character \u200B
.
if you pressed backspce on a textfield that appears empty, it will actually delete your space. Then you can just reinsert the space. but caret not moves it still sit on the same position but it deletes space.
or simply you can check for length
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"stringsss."] invertedSet];
if (range.length == 1) {
return string;
}
else {
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0);
}
}
check answers of these questions asked so many time. one

Community
- 1
- 1

Ravindra Bagale
- 17,226
- 9
- 43
- 70