开发者

How to move the cursor to next UITextField programmatically?

开发者 https://www.devze.com 2022-12-13 07:42 出处:网络
I am developing an application where I am creating some UITextField programmatically, and I allow a maximum 1 character in the UITextField.

I am developing an application where I am creating some UITextField programmatically, and I allow a maximum 1 character in the UITextField.

This is working perfectly.

But after writing the character, the user has to again tap in the next UITextField to present the keyboard

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

H开发者_JS百科ow can I do that?


Catch the delegate that informs you your textfield has been changed. Check if the new length is 1. If it is, call becomeFirstResponder on the next field.


I have found this approach..

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

We can do it in another way... Declare this line in viewDidLoad and that method.

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

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

Hope this helps...


Above solution did not work. This solution does work for me:

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


This can be done in 3 simple steps:

  1. The current text field should resign for the cursor.
  2. Calculate the following text field
  3. Make the following text field gain the cursor.

In code:

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

Hope it helps.


just set

UITextFieldDelegate

and past this method in your .m file. it will works For OTP text Boxes.

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

Hope this work for anyone. :)


you've to just use

[textField'sName becomeFirstResponder];
0

精彩评论

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