//在现实开发中 需要控制文本输入长度 并实时做短信验证,代码如下
[self.textField addTarget:self action:@selector(codeChange:) forControlEvents:UIControlEventEditingChanged];
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.textField) {
if (range.length == 1 && string.length == 0) {
return YES;
}
else if (self.textField.text.length >= 6) {
self.textField.text = [textField.text substringToIndex:6];
return NO;
}
}
return YES;
}
//注意:事件类型是:`UIControlEventEditingChanged`
-(void)codeChange:(id)sender{
if ([CommonTool isBlank:self.code_id]) {
[MBProgressHUD showError:@"请先获取验证码"];
return;
}
UITextField *target=(UITextField*)sender;
if (target.text.length == 6) {
///执行相对应的操作及网络请求
}
}
原文地址:https://www.cnblogs.com/tryFighting/p/10556811.html