这是我的代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL isPressedBackspaceAfterSingleSpaceSymbol = [string isEqualToString:@""] && [resultString isEqualToString:@""] && range.location == 0 && range.length == 1;
if (isPressedBackspaceAfterSingleSpaceSymbol) {
if (textField == self.txt1 ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt1 resignFirstResponder];
});
} else if (textField == self.txt2 ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt1 becomeFirstResponder];
});
} else if (textField == self.txt3 ) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt2 becomeFirstResponder];
});
} else if (textField == self.txt4) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.txt3 becomeFirstResponder];
});
}
} else {
dispatch_async(dispatch_get_main_queue(), ^{
if (textField == self.txt1 ) {
dispatch_async(dispatch_get_main_queue(), ^{
self.txt1.text = string;
[self.txt2 becomeFirstResponder];
});
if (self.txt1.text.length == 1) {
[self.txt2 becomeFirstResponder];
}
} else if (textField == self.txt2 ) {
dispatch_async(dispatch_get_main_queue(), ^{
self.txt2.text = string;
[self.txt3 becomeFirstResponder];
});
if (self.txt2.text.length == 1) {
[self.txt3 becomeFirstResponder];
}
} else if (textField == self.txt3 ) {
dispatch_async(dispatch_get_main_queue(), ^{
self.txt3.text = string;
[self.txt4 becomeFirstResponder];
});
if (self.txt3.text.length == 1) {
[self.txt4 becomeFirstResponder];
}
} else if (textField == self.txt4) {
dispatch_async(dispatch_get_main_queue(), ^{
self.txt4.text = string;
[self.txt4 resignFirstResponder];
});
if (self.txt4.text.length == 1) {
[self.txt4 resignFirstResponder];
}
}
});
}
return true;
}
我正面临越界错误。我不知道怎么办。
崩溃,显示行号186 self.txt1.text = string;
注意:-这是我的otp视图控制器。我有4个otp的4个文本字段。我允许用户在文本字段中仅输入数字。一个文本字段中只有一个数字。
谢谢。