0

How do I identify when user press keys in keyboard? What is delegate method for it?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
user2003416
  • 159
  • 2
  • 9

2 Answers2

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;
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
iPatel
  • 46,010
  • 16
  • 115
  • 137
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

two

three

Community
  • 1
  • 1
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70