开发者

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

开发者 https://www.devze.com 2023-03-23 06:19 出处:网络
This question already has answers here: Detect backspace in empty UITextField (28 answers) Closed 9 years ago.
This question already has answers here: Detect backspace in empty UITextField (28 answers) Closed 9 years ago.

How can i detect backspace button pres开发者_运维百科sed on UIKeyboard?

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

thanks


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


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.


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;
}


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消