Detect backspace in empty UITextField

2019-01-01 05:16发布

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.

23条回答
墨雨无痕
2楼-- · 2019-01-01 05:17

In .h file add UIKeyInput delegate

- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {

if ([textField isEqual:_txtFirstDigit]) {

}else if([textField isEqual:_txtSecondDigit]) {
    [_txtFirstDigit becomeFirstResponder];

}else if([textField isEqual:_txtThirdDigit]) {
    [_txtSecondDigit becomeFirstResponder];

}else if([textField isEqual:_txtFourthDigit]) {
    [_txtThirdDigit becomeFirstResponder];
}
return YES;
}   

improved Formatting

查看更多
零度萤火
3楼-- · 2019-01-01 05:19

Using the TextField Delegate method:

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

Add the following code in above method to detect delete event

if(textField == YourTextField)
{
    if ([string length] == 0 && range.length > 0)
    {
        // Your Code after deletion of character
    }
}
查看更多
还给你的自由
4楼-- · 2019-01-01 05:21

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.

查看更多
零度萤火
5楼-- · 2019-01-01 05:21

Rather than trying to preconstruct what WILL BE in the text field or figure out what special character has been entered in the shouldChangeCharactersInRange method, I would suggest doing the following:

[self performSelector:@selector(manageSearchResultsDisplay) 
           withObject:nil 
           afterDelay:0];

This allows you to call a method directly after the current operation completes. What's cool about this is that, by the time it completes, the modified value will already be in the UITextField. At that point, you can just check its length and/or validate based on what's there.

查看更多
素衣白纱
6楼-- · 2019-01-01 05:21
+ (BOOL)detectBackspaceOnly:(NSString *)string
{
    for(int i=0 ; i<string.length ; i++){
        unichar caract = [string characterAtIndex:i];
        if(caract != ' ' && caract != '\n')
            return NO;
    }

    return YES;
}
查看更多
墨雨无痕
7楼-- · 2019-01-01 05:23

Yup, use below method to detect backspace, when textField is empty.

Need to add UITextFieldDelegate

yourTextField.delegate = self (MUST REQUIRED)

Swift:

func keyboardInputShouldDelete(_ textField: UITextField) -> Bool { 
    return true
}

Objective C:

- (BOOL)keyboardInputShouldDelete:(UITextField *)textField { 
    return YES; 
}
查看更多
登录 后发表回答