如何使 UITextField 只有一个 text/number 并使用 Next / Done 按钮浏览多个 UITextField

How can I enable the UITextField to have only one text/number and navigate through multiple `UITextField`s using Next / Done Button

我必须使用(下一步/完成按钮)处理多个 UITextField 的导航,现在我必须在每个 UITextField 中只允许一个 text/number 我们如何才能做到这一点在 UITextField 中,如下图所示

我最近使用了以下代码并且也能够实现但是遇到了问题当我第一次在 UITextField 中输入 text/number 时它被输入 UITextField 并且当我第二次进入下一个 text/number 它仅用于推送到下一个 UITextField。我想要实现的是,当我第二次输入 text/number 时,它必须被推到下一个 UITextField 并且必须在下一个 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;

}

要在文本字段中仅输入数字并设置文本字段的长度,试试这个:

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

你可以通过使用 textfield delegate 方法来做到这一点,你可以按顺序为每个文本字段设置标签,如 1,2,3…。现在在 shouldChangeCharactersInRange

当您在文本框中键入 text/number 时,使下一个框成为第一响应者的方法正确逻辑。

如下所示

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

编辑:

如果你想在文本字段的跳转中显示输入文本,你可以在 [nextResponder becomeFirstResponder]

之后添加这些行

添加下面给出的这些行

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

编辑最后一个文本字段中的键盘辞职响应程序。

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;

}

这里我们检查最后一个文本标签在这里我检查 4 标签值你把你最后一个 textfiedl 标签放在你输入最后一个文本字段的值时它会退出键盘.希望对你有帮助。

设置文本字段的委托并实现以下委托方法:

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