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

综合技术 2017-12-26

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!

责编内容by:Hello, buddy! (源链)。感谢您的支持!

您可能感兴趣的

Note the results of 10 in UITextField? I am trying to create a UITextField that users can enter a number between 1-10 in order to give a sc...
How do I get rid of & ldquo; Ghost &a... I have a strange problem with a UITextField - I am setting the value of it to @"" (or anything else ...
Only Alphabet and white space allowed in UITextFie... I want to validate my textField that check only Alphabet & whitespace allowed, If number was ...
SizeToFit Navigation Bar Element (UITextF... How do I size a UITextField to fit the whole UINavigationBar? So far I have this: locationField = ...
Change the behavior of UITextField What is the best way to disable UITextField's 'return' keyboard key if input area does not contain ...