Ios 为UITextField创建通用下一步按钮

Ios 为UITextField创建通用下一步按钮,ios,objective-c,for-loop,tags,uitextfield,Ios,Objective C,For Loop,Tags,Uitextfield,我正在尝试创建一个与textField的标记一起工作的next按钮。 我在一个void类型方法中有这样一个方法: for (UITextField *textField in self.view.subviews) { if ([textField isKindOfClass:[UITextField class]]) { if([textField isFirstResponder]) { int i = _textFie

我正在尝试创建一个与textField的标记一起工作的next按钮。 我在一个void类型方法中有这样一个方法:

for (UITextField *textField in self.view.subviews)
{
    if ([textField isKindOfClass:[UITextField class]])
    {
        if([textField isFirstResponder])
        {
            int i = _textFieldTag;  //starts as 0

            [[textField viewWithTag:i] resignFirstResponder];
            NSString *a = [(UITextField *)[textField viewWithTag:i] text];
            NSLog(@"TEXT 01 - %@", a);

            i = i + 1;
            NSLog(@"TAG 02 - %i", i);

            [[textField viewWithTag:i] becomeFirstResponder];
            NSString *b = [(UITextField *)[textField viewWithTag:i] text];
            NSLog(@"TEXT 02 - %@", b);
        }
    }
}
问题是,即使
i
增加1,
NSString*b
返回nil,并且不会像我预期的那样使该文本字段成为下一个响应者

它们在那里,标签存在,但不知何故,i的新值不被接受


有什么想法吗?

试着从以下位置替换所有电话:

[textField viewWithTag:i]
致:


您应该要求查看带有标记的视图而不是UITextField。

您的逻辑不正确,正如@Greg所说,您需要使用
[self.view viewWithTag:i]
来获得正确的视图。另外,为self.view指定一个不同于任何文本字段标记的标记(由于self.view的默认标记为0,所以出现了错误)。我想你希望你的逻辑是这样的:

-(IBAction)nextField:(id)sender {
    int i;
    for (UITextField *textField in self.view.subviews) {
        if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
            [textField resignFirstResponder];
            NSString *a = textField.text;
            NSLog(@"TEXT 01 - %@", a);
            //i = textField.tag + 1; // use this line if you don't want to go back to the first text field
            i = (textField.tag == 4)? 0 : textField.tag + 1; //use this line if you want to cycle back to the first text field when you are on the last one. Replace the 4 with whatever is your highest tag. This also assumes that the tag of the first text field is 0.  
        }
    }

    NSLog(@"TAG 02 - %i", i);
    [[self.view viewWithTag:i] becomeFirstResponder];
    NSString *b = [(UITextField *)[self.view viewWithTag:i] text];
    NSLog(@"TEXT 02 - %@", b);
}

很抱歉导致应用程序崩溃***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[UIView text]:无法识别的选择器已发送到实例0x8c15ca0'
-(IBAction)nextField:(id)sender {
    int i;
    for (UITextField *textField in self.view.subviews) {
        if ([textField isKindOfClass:[UITextField class]] && [textField isFirstResponder]) {
            [textField resignFirstResponder];
            NSString *a = textField.text;
            NSLog(@"TEXT 01 - %@", a);
            //i = textField.tag + 1; // use this line if you don't want to go back to the first text field
            i = (textField.tag == 4)? 0 : textField.tag + 1; //use this line if you want to cycle back to the first text field when you are on the last one. Replace the 4 with whatever is your highest tag. This also assumes that the tag of the first text field is 0.  
        }
    }

    NSLog(@"TAG 02 - %i", i);
    [[self.view viewWithTag:i] becomeFirstResponder];
    NSString *b = [(UITextField *)[self.view viewWithTag:i] text];
    NSLog(@"TEXT 02 - %@", b);
}