How to programmatically move the cursor to the next UITextField?

I am developing an application in which I create some UITextFieldprogrammatically and I allow a maximum of 1 character per UITextField.

This works great.

But after writing the character, the user must click again in the next UITextFieldto present the keyboard

After entering 1 character, the cursor should automatically move to the next UITextField.

How can i do this?

+5
source share
6 answers

Catch the delegate who reports that your text box has been changed. Make sure the new length is 1. If so, call becomeFirstResponderin the next field.

+16

.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(textField == textField1)
    {
        if (textField1.text.length > 0  && range.length == 0 )
        {
            return NO;
        }
        else {
            [textField1 setText:newString];
            [textField2 becomeFirstResponder];
            return YES;
        }
    }
    else if(textField == textField2)
    {
        if (textField2.text.length > 0  && range.length == 0 )
        {
            return NO;
        }else {
            [textField2 setText:newString];
            [textField3 becomeFirstResponder];
            return YES;
        }
    }
}

-... viewDidLoad .

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditingTextField:) name:@"UITextFieldTextDidChangeNotification" object:txtfield1];

- (void) endEditingTextField:(NSNotification *)note {
    if ([[txtfield1 text] length] > 0)
    {
        [txtfield1 resignFirstResponder];
        [txtfield2 becomeFirstResponder];
    }
}

, ...

+4

. :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(firstCodeField == textField){
        [firstCodeField setText:newString];
        [secondCodeField becomeFirstResponder];
    }
    if(secondCodeField == textField){
        [secondCodeField setText:newString];
        [thirdCodeField becomeFirstResponder];
    }

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 1) ? NO : YES;
 }
+3

:

  • .
  • , .

:

if ([currentTextFieldWithCursor canResignFirstResponder]) [currentTextFieldWithCursor resignFirstResponder];
UITextField *followingTextField = // just get it!
if ([followingTextField canBecomeFirstResponder]) [followingTextField becomeFirstResponder];

, .

+1

UITextFieldDelegate

.m . OTP.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

    if(_txt1 == textField){
        if ([_txt1.text length] == 0) {
             [_txt1 setText:newString];
               [_txt2 becomeFirstResponder ];
        }

    }
    if(_txt2 == textField){

        if ([_txt2.text length] == 0) {
              [_txt2 setText:newString];
              [_txt3 becomeFirstResponder];
        }

    }

    if(_txt3 == textField){

         if ([_txt3.text length] == 0) {
              [_txt3 setText:newString];
               [_txt4 becomeFirstResponder];
         }

    }

    if(_txt4 == textField){

        if ([_txt4.text length] == 0) {
             [_txt4 setText:newString];
        }

    }

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 1 && newLength == 2) ? NO : YES;
}

, .:)

+1

[textField'sName becomeFirstResponder];
0

All Articles