How do I enable the UITextField to have a single text / number and navigate through mul…

I have to handle navigate through multiple UITextField s using (Next / Done Button) and now I have to allow only one text/number in each UITextField how could we do that in UITextField as shown in image below

I have used following code recently and was able to achieve too but got problem that When I first enter the text/number in UITextField it is being entered in UITextField and when I enter the next text/number for second time it is being used only to push to next UITextField . I want to achieve is that when i enter the text/number for second time it must be pushed to next UITextField as well as value must be entered in next UITextField

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag= 1) && (string.length > 0))
        {

            NSInteger nextText = textField.tag + 1;
            // Try to find next responder
            UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
            if (! nextResponder)
                [textField resignFirstResponder];
               // nextResponder = [textField.superview viewWithTag:1];

            if (nextResponder){
                // Found next responder, so set it.
                [nextResponder becomeFirstResponder];

                return NO;

            }

        }
    }

    return YES;

}

I solve my problem using following code but I got a one more problem left. For the last UITextField i need it to resignFirstResponder immediately after it is filled up. But in current when it is fill up and user click next text then only resignFirstResponder is happening

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag= 1) && (string.length > 0))
        {

            NSInteger nextText = textField.tag + 1;
            UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
            if (! nextResponder)
                [textField resignFirstResponder];
               if (nextResponder){
                    [nextResponder becomeFirstResponder];

                    UITextField* nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText];

                    if ((nextTextfield.text.length < 1)){
                    [nextTextfield setText:string];
                    }
                    return NO;
                 }

        }
    }

    return YES;

}

you can do this by using textfield delegate Method you can set tags to each text field in sequence like 1,2,3…. now in shouldChangeCharactersInRange

Method right logic that make next box to become first responder when you type one text/number in text box.

like given below

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

    if ((textField.text.length >= 1) && (string.length > 0))
       {

            NSInteger nextText = textField.tag + 1;
            // Try to find next responder
            UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
        if (! nextResponder)
            nextResponder = [textField.superview viewWithTag:1];

           if (nextResponder)
               // Found next responder, so set it.
               [nextResponder becomeFirstResponder];

        return NO;
        }
    return YES;
    }

Edit:

If you want to Display enter text alos on jump of textfield you can add these lines after [nextResponder becomeFirstResponder]

add these lines given below

UITextField nextTextfield= (UITextField) [textField.superview viewWithTag:nextText];
    [nextTextfield setText:string];

EDIT for resign responder for keyboard in last textfield.

If you want resign responder for last textfield 

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag= 1) && (string.length > 0))
        {

            NSInteger nextText = textField.tag + 1;
            // Try to find next responder
            UIResponder* nextResponder = [textField.superview viewWithTag:nextText];
            if (! nextResponder)
                [textField resignFirstResponder];
            // nextResponder = [textField.superview viewWithTag:1];

            if (nextResponder){
                // Found next responder, so set it.
                [nextResponder becomeFirstResponder];
                UITextField *nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText];
                if (nextTextfield.text.length<1) {
                    if(nextTextfield.tag==4){
                        [nextTextfield setText:string];
                        [nextTextfield resignFirstResponder];
                    }else{
                        [nextTextfield setText:string];
                    }

                }

                return NO;

            }

        }
    }

    return YES;

}

This in this we check for last text tag here i check for 4 tag value u put ur last textfiedl tag in condition when u enter value for last textfield it will resign keyboard. hope this will help you.

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » How do I enable the UITextField to have a single text &sol; number and navigate through mul…

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录