【发布时间】: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