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

后端 未结 10 1458
余生分开走
余生分开走 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:46
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
       const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
       int isBackSpace = strcmp(_char, "\b");
    
       if (isBackSpace == -8) {
         NSLog(@"Backspace was pressed");
       }
    
    return YES;
    
    }
    

    Basically this method detects which button you are pressing (or have just pressed). This input comes in as an NSString. We convert this NSString to a C char type and then compare it to the traditional backspace character (\b). Then if this strcmp is equal to -8, we can detect it as a backspace.

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

    Swift 2.0 version for Detecting BackSpace based deletion, referencing code post from almas

    //For Detecting Backspace based Deletion of Entire Word in TextField
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool { 
        if (range.length == 1 && string.isEmpty){
            print("Used Backspace")
        }
    return true
    }
    
    0 讨论(0)
  • 2020-11-30 01:51

    Swift 2.2:

    func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    
        if text == "" {
          print("Backspace has been pressed")
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-11-30 01:58

    A lot of people have been saying this is a bug, but being that this problem still exists in the GM I'm starting to think it might be a change in logic. With that said, I wrote this bit of code for my app and have tested it on iOS 7-8.

    Add the following method to your UITextField subclass.

    - (BOOL)keyboardInputShouldDelete:(UITextField *)textField {
        BOOL shouldDelete = YES;
    
        if ([UITextField instancesRespondToSelector:_cmd]) {
            BOOL (*keyboardInputShouldDelete)(id, SEL, UITextField *) = (BOOL (*)(id, SEL, UITextField *))[UITextField instanceMethodForSelector:_cmd];
    
            if (keyboardInputShouldDelete) {
                shouldDelete = keyboardInputShouldDelete(self, _cmd, textField);
            }
        }
    
        BOOL isIos8 = ([[[UIDevice currentDevice] systemVersion] intValue] == 8);
        BOOL isLessThanIos8_3 = ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.3f);
    
        if (![textField.text length] && isIos8 && isLessThanIos8_3) {
            [self deleteBackward];
        }
    
        return shouldDelete;
    }
    

    This code is slightly before the red line of private API's, however you should have no problem using it. My app with this code is in the app store.

    To explain a little, were calling the super implementation of this method to avoid losing code. After were going to call -deleteBackward if there is no text and the iOS version is between 8-8.2.

    EDIT: 1/22/15

    It also might be helpful to subclass the -deleteBackward method of your subclassed UITextField. This fixes a few conditional bugs. One being if you use a custom keyboard. Heres an example of the method.

    - (void)deleteBackward {
        BOOL shouldDismiss = [self.text length] == 0;
    
        [super deleteBackward];
    
        if (shouldDismiss) {
            if ([self.delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
                [self.delegate textField:self shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""];
            }
        }
    }
    

    EDIT: 4/13/15

    As @Gee.E commented, iOS 8.3 has fixed this issue. The code has been updated to reflect the changes.

    0 讨论(0)
提交回复
热议问题