【问题标题】:Text field one character and automatic change to next text field?Not working?文本字段一个字符并自动更改为下一个文本字段?不起作用?
【发布时间】:2018-01-11 05:43:00
【问题描述】:

我有一个程序,其中有四个文本字段,每个文本字段只需要一个字符即可采用 OTP。当用户在一个文本字段中输入一个字符时,它应该自动移动到下一个文本字段。但由于某种原因,只有我的第一个文本字段能够执行此操作,并且它转到第三个文本字段而不是第二个。并且文本字段的其余部分无法执行从一个文本字段移动到另一个文本字段的自动操作。我使用文本字段委托进行编码。下面给出的是我的代码。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (![string isEqualToString:@""]) {
    textField.text = string;
    if ([textField isEqual:self.firstOTP]) {
        [self.secondOTP becomeFirstResponder];
    }else if ([textField isEqual:self.secondOTP]){
        [self.thirdOTP becomeFirstResponder];
    }else if ([textField isEqual:self.thirdOTP]){
        [self.fourthOTP becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return NO;
}
return YES;
}

下面是我的代码,用于识别是否输入了一个字符。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
if (textField.text.length > 0) {
    textField.text = @"";
}
return YES;
}

任何人都可以识别错误吗?

【问题讨论】:

  • 是否为所有四个文本字段设置了委托?您应该将if ([textField isEqual:self.firstOTP]) 更改为if (textField == self.firstOTP)(以及其他两个)。您实际上想比较指针。
  • 是的,它是为所有文本字段设置的。
  • 目前它正在从第一个文本字段到第三个文本字段。 @rmaddy

标签: ios objective-c uitextfield


【解决方案1】:

首先,确保为所有四个文本字段设置了delegate。还要确保您的插座连接正确。

接下来,将您对 isEqual: 的使用更改为 ==。您实际上确实想在这里使用==,因为您想比较指针,而不是查看两个对象在逻辑上是否相等。

如果用户将文本粘贴到文本字段中,您也会遇到问题。用户可以轻松输入多个字符。

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (string.length > 0) {
        textField.text = [string substringToIndex:1];
        if (textField == self.firstOTP) {
            [self.secondOTP becomeFirstResponder];
        } else if (textField == self.secondOTP) {
            [self.thirdOTP becomeFirstResponder];
        } else if (textField == self.thirdOTP) {
            [self.fourthOTP becomeFirstResponder];
        } else {
            [textField resignFirstResponder];
        }

        return NO;
    }

    return YES;
}

【讨论】:

  • 问题依然存在。不用找了。 @rmaddy
  • 您是否确认self.secondOTP 确实指向第二个文本字段?使用调试器,看看当你在第一个文本字段中输入一个字符时会发生什么。对[self.secondOTP becomeFirstResponder]; 的调用是否按预期到达?
  • 这个问题很愚蠢。我将 seconOTP 分配给 ThirdOTP 文本字段时出错。但是复制问题仍然存在。如何解决。 @rmaddy
  • 好的,我回答的第二句话解决了这个问题。你刚才提到的复制问题是什么?您的问题与此无关。
  • 我不想在我的文本字段中启用粘贴功能。
【解决方案2】:

试试这个,它将帮助您进入下一个文本字段并在删除时返回 我希望这会有所帮助

 -(void)textFieldDidChange :(UITextField *) textField{
if (textField == FirstChar) {
    if (textField.text.length==0) {
        [textField becomeFirstResponder];
        [FirstChar becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
        [SecondChar becomeFirstResponder];
    }
}
else if (textField == SecondChar) {
    if (textField.text.length==0) {
        [textField resignFirstResponder];

        [SecondChar becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
        [ThirdChar becomeFirstResponder];
    }
}
else if (textField == ThirdChar) {
    if (textField.text.length==0) {
        [textField resignFirstResponder];

        [ThirdChar becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
        [FourthChar becomeFirstResponder];
    }

}


}

【讨论】:

  • 1.在拨打becomeFirstResponder 之前,您无需拨打resignFirstResponder。 2. 你所有的内部if 语句在textfield 上调用resignFirstResponder 然后在同一个文本字段上调用becomeFirstResponder 是没有意义的。
【解决方案3】:

需要记住的重要事情

1) 最初,您必须设置 UITextField 的委托 2) 确保所有文本字段的出口连接正确。

举个例子:

1) 假设您采用了 4 个文本字段,例如 firstPin、secondPin、thirdPin、fourthPin 2)现在在委托函数“shouldChangeCharactersInRange”中,你必须让 txtOne 成为第一个响应者,而其他所有你必须让 resignFirstResponder

这里是代码

- (BOOL)textField:(UITextField )textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString )string
{    
if (range.length==1)
{
    [textField setText:@""];

    if (textField == firstPin)
    {
        NSLog(@"dumbo");
    }
    else if (textField == secondPin)
    {
        [secondPin resignFirstResponder];
        [firstPin becomeFirstResponder];
    }
    else if (textField == thirdPin)
    {
        [thirdPin resignFirstResponder];
        [secondPin becomeFirstResponder];
    }
    else if (textField == fourthPin)
    {
        [fourthPin resignFirstResponder];
        [thirdPin becomeFirstResponder];
    }
}
else
{
    [textField setText:string];

    if (textField == firstPin)
    {
        [firstPin resignFirstResponder];
        [secondPin setUserInteractionEnabled:YES];
        [secondPin becomeFirstResponder];
    }
    else if (textField == secondPin)
    {
        [secondPin resignFirstResponder];
        [thirdPin setUserInteractionEnabled:YES];
        [thirdPin becomeFirstResponder];
    }
    else if (textField == thirdPin)
    {
        [thirdPin resignFirstResponder];
        [fourthPin setUserInteractionEnabled:YES];
        [fourthPin becomeFirstResponder];
    }
    else if (textField == fourthPin)
    {
        [fourthPin resignFirstResponder];
    }


}

return NO;

}

此代码支持添加文本和删除文本

【讨论】:

  • 这基本上是我答案的副本。这个答案如何提供任何额外的信息?你在shouldChangeCharactersInRange 中的大部分代码都是错误的。没有理由在致电becomeFirstResponder 之前致电resignFirstResponder。你为什么要查看range.length?如果我在文本字段中选择一个现有字符,然后键入一个新字符,您的代码将简单地清除文本字段,然后转到上一个文本字段。这根本不对。您的代码无法处理用户粘贴过多文本。
猜你喜欢
  • 2018-08-30
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多