I have a OTP Screen, where I have to enter the OTP in 4 digit password in 4 Diff TextFields, The scenario is like this :
I have managed the working up to 70% but back space works only when the user enters all the textField. I'm pasting out my code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// This allows numeric text only, but also backspace for deletes
if (string.length > 0 && ![[NSScanner scannerWithString:string] scanInt:NULL])
return NO;
NSUInteger oldLength = [textField.text length];
NSUInteger replacementLength = [string length];
NSUInteger rangeLength = range.length;
NSUInteger newLength = oldLength - rangeLength + replacementLength;
// This 'tabs' to next field when entering digits
if (newLength == 1) {
if (textField == _pin1)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin2 afterDelay:0];
}
else if (textField ==_pin2)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin3 afterDelay:0];
}
else if (textField == _pin3)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin4 afterDelay:0];
}
}
//this goes to previous field as you backspace through them, so you don't have to tap into them individually
else if (oldLength > 0 && newLength == 0) {
if (textField ==_pin4)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin3 afterDelay:0];
}
else if (textField == _pin3)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin2 afterDelay:0];
}
else if (textField == _pin2)
{
[self performSelector:@selector(setNextResponder:) withObject:_pin1 afterDelay:0];
}
}
return newLength <= 1;
}
- (void)setNextResponder:(UITextField *)nextResponder
{
[nextResponder becomeFirstResponder];
}
working:
Need to give tag into UITextView
in StoryBoard
.
Like textField1 = 101,textField2 = 102,textField3 = 103,textField4 = 104
Put following code into your viewcontroller
.
- (BOOL)keyboardInputShouldDelete:(UITextField *)textField {
BOOL shouldDelete = YES;
if ([textField.text length] == 0 && [textField.text isEqualToString:@""]) {
long tagValue = textField.tag - 1;
UITextField *txtField = (UITextField*) [self.view viewWithTag:tagValue];
[txtField becomeFirstResponder];
}
return shouldDelete;
}
Following code for the next UItextField
Focus.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// This allows numeric text only, but also backspace for deletes
if (string.length > 0 && ![[NSScanner scannerWithString:string] scanInt:NULL])
return NO;
if ([textField.text length] == 0) {
[self performSelector:@selector(changeTextFieldFocusToNextTextField:) withObject:textField afterDelay:0.3];
}
return YES;
}
-(void)changeTextFieldFocusToNextTextField:(UITextField*)textField{
long tagValue = textField.tag + 1;
UITextField *txtField = (UITextField*) [self.view viewWithTag:tagValue];
[txtField becomeFirstResponder];
}
Sample Output
Grab the solution demo project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With