How to detect UIKeyboard “backspace” button touch on UITextField? [duplicate]

假装没事ソ 提交于 2019-12-28 04:01:09

问题


How can i detect backspace button pressed on UIKeyboard?

thanks


EDIT: Is there any keyboard delegate that returns key pressed value?


回答1:


if the UITextField is empty, the shouldChangeTextInRange delegate method is not called You can subclass UITextField and override this method:

-(void)deleteBackward;
{
    [super deleteBackward];
    NSLog(@"BackSpace Detected");
}

since UITextField is compliant to the UITextInput protocol, this method is implemented, and you can override it to detect when backspace is pressed. Then, you can write your own protocol/delegate method to alert your custom textfield delegate if the backspace is detected.




回答2:


First of all your UIViewController that references the UITextField needs to conform to the UITextFieldDelegate protocol. Then set your class as delegate to the UITextField (eg. [myTextField setDelegate:self] ). Then in your class you add the following. When a string of "" is sent as the replacementString you know it's backspace.

(BOOL)textField:(UITextField *)textField 
      shouldChangeCharactersInRange:(NSRange)range 
      replacementString:(NSString *)string
{                
    if ([string isEqualToString:@""]) {
        NSLog(@"Backspace");            
    }
    return YES;
}



回答3:


Temporary i'm inserting a zero space char on textFieldDidBeginEditing

textField.text = @"\u200B";

on backspace, it remove that chars, but graphically it's the same!

It's an hack, it works, but it hide placeholder text... not good...



来源:https://stackoverflow.com/questions/6818342/how-to-detect-uikeyboard-backspace-button-touch-on-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!