Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

How can i detect backspace button pressed on UIKeyboard?

enter image description here

thanks


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

like image 413
elp Avatar asked Jul 25 '11 15:07

elp


3 Answers

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.

like image 171
LombaX Avatar answered Oct 17 '22 00:10

LombaX


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;
}
like image 30
user2457847 Avatar answered Oct 17 '22 00:10

user2457847


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

like image 24
elp Avatar answered Oct 16 '22 22:10

elp