3

I have to handle navigate through multiple UITextFields 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

enter image description here

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<10) {

        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)
                [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<10) {

        if ((textField.text.length >= 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;

}
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57

3 Answers3

3

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<10) {

        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)
                [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.

adricadar
  • 9,971
  • 5
  • 33
  • 46
user1548843
  • 670
  • 12
  • 20
  • I have modified the question can you help me out to solve the recent problem – Nischal Hada May 05 '15 at 07:12
  • Hi you can do this by add this two UITextField *nextTextfield= (UITextField*) [textField.superview viewWithTag:nextText]; [nextTextfield setText:string]; line after [nextResponder becomefirstResponder]; line edited answer – user1548843 May 05 '15 at 11:19
  • It is working but when all 4 textfield of row is already full and user again enter the text in textfield it starts changing text of next textfield. I want to stop that – Nischal Hada May 05 '15 at 12:39
  • When all 4 textfield of row is full. it jump to next row 1st textfield. you don't want this behaviour? – user1548843 May 05 '15 at 13:46
  • Every things iss alright only when UITextField are empty I didnt want to wrte in same UITextField when UITextField is already full – Nischal Hada May 06 '15 at 01:58
  • If you want than you can put check like if (nextTextfield.text.length<1) { [nextTextfield setText:string]; } if text field is empty than and only than you enter text in textfield. Hope this is help you. – user1548843 May 06 '15 at 12:02
  • do u have solutions 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 – Nischal Hada Jun 20 '15 at 09:25
  • @nischalhada please find edited answer hope it would be as per your requirement.if not let me know. hope it would be helpful for you. – user1548843 Jun 22 '15 at 07:06
  • I have already try that but doesnt work out I was thinking since we have return NO; so resignFirstResponder might not be working – Nischal Hada Jun 22 '15 at 07:16
  • @nischal hada: Edit code I try and it's working for me. r u resining current textfield or nextTextfield textfield? i don't think returen NO create problem if so in this condition part return Yes like this and try if(nextTextfield.tag==4){ [nextTextfield setText:string]; [nextTextfield resignFirstResponder];return YES, } – user1548843 Jun 22 '15 at 07:25
  • Gr8 enjoy coding my friend. – user1548843 Jun 22 '15 at 07:32
  • do u have any ideas if i have a textfield and it need to handell resignFirstResponder when it has exactly 10 text. I use if (textField.text.length >= 10 && range.length == 0) { [textField resignFirstResponder]; return YES; // return NO to not change text } it works only when i clicked 11th resignFirstResponder works But I want to acheive excatly on 10th text – Nischal Hada Jul 02 '15 at 10:07
2

To enter only numbers in the your text field and to set length of the text field,try this:

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

// Check for non-numeric characters
NSUInteger lengthOfString = string.length;
for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {
    unichar character = [string characterAtIndex:loopIndex];
    if (character < 48) return NO; // 48 unichar for 0
    if (character > 57) return NO; // 57 unichar for 9
}

// Check for total length
NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
if (proposedNewLength > 1) return NO;     //set your length here
return YES;
}
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
1

setting the text field's delegate and implementing the following delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    // Prevent crashing undo bug – see note below.
    if(range.length + range.location > textField.text.length)
    {
        return NO;
    }

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

Set the maximum character length of a UITextField

Community
  • 1
  • 1