How to detect delete key on an UITextField in iOS 8?

后端 未结 10 1456
余生分开走
余生分开走 2020-11-30 00:56

I have subclassed UITextField and implemented the UIKeyInput protocol\'s deleteBackward method to detect backspace being pressed. This works fine on iOS 7 but not on iOS 8.<

相关标签:
10条回答
  • 2020-11-30 01:31

    This does not explicitly answer the original question but worth nothing that in the documentation for textField(_:shouldChangeCharactersIn:replacementString:), it says:

    "string: The replacement string for the specified range. During typing, this parameter normally contains only the single new character that was typed, but it may contain more characters if the user is pasting text. When the user deletes one or more characters, the replacement string is empty."

    Thus, we can detect backspaces in a UITextFieldDelegate if we implement textField(_:shouldChangeCharactersIn:replacementString:) and check if the length of string is 0.

    A lot of other answers here have used this same logic without referencing the documentation so hopefully getting it right from the source makes people more comfortable using it.

    0 讨论(0)
  • 2020-11-30 01:33

    You must look an example for MBContactPicker on github. Deletion of contacts at MBContactPicker via Backspace button on iOS8 tested by me. And it works greatly! You can use its as example.

    Author of MBContactPicker use next method: When UITextField must become empty (or before call becomeFirstResponder when it is empty), he save single whitespace symbol there. And then when you press Backspace button (when focus was set to end of text of your UITextField), method

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

    will work. Inside it you must use check like this:

    NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    BOOL isPressedBackspaceAfterSingleSpaceSymbol = [string isEqualToString:@""] && [resultString isEqualToString:@""] && range.location == 0 && range.length == 1;
    if (isPressedBackspaceAfterSingleSpaceSymbol) {
        //  your actions for deleteBackward actions
    }
    

    So, you must always control that UITextField contains single whitespace.

    This is not hack. So, user willn't noticed about some behaviour was changed

    0 讨论(0)
  • 2020-11-30 01:35

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

    This function is called when you hit delete key

    0 讨论(0)
  • 2020-11-30 01:40

    In iOS8, some custom keyboards delete whole word, so only check string.length is OK.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (string.length==0) { //Delete any cases
           if(range.length > 1){
              //Delete whole word
           }
           else if(range.length == 1){
              //Delete single letter
           }
           else if(range.length == 0){
              //Tap delete key when textField empty
           }  
        }  
        return YES;
    }
    
    0 讨论(0)
  • 2020-11-30 01:42

    swift 2:

    if (string.characters.count == 0 && range.length == 1) {
                return true
    }
    

    you should use like this string.characters.count

    0 讨论(0)
  • 2020-11-30 01:44

    You can detect when user deletes text by using backspace by implementing UITextField delegate method:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if (range.length==1 && string.length==0)
            NSLog(@"backspace tapped");
    
        return YES;
    }
    
    0 讨论(0)
提交回复
热议问题