我想在文本字段 ( codeA
) 中写入 4 个数字,如果里面有 4 个数字,我想选择另一个字段 ( codeB
)。我希望 codeB 也限制为 4 个数字。但如果我将它设为 View Controller 的委托,应用程序就会崩溃。我在.h中的代码:
@property (weak, nonatomic) IBOutlet UITextField *codeA;
@property (weak, nonatomic) IBOutlet UITextField *codeB;
我在 .m 中的代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ([codeA.text length] > 3) {
codeA.text = [textField.text substringToIndex:4];
[codeB becomeFirstResponder];
return NO;
}
if ([codeB.text length] > 3) {
codeB.text = [codeB.text substringToIndex:4];
return NO;
}
return YES;
}
如果codeB
不是 View Controller 的委托,它可以工作,但 B 不受限制。