iOS 设置textfield的最大文本长度

//在现实开发中  需要控制文本输入长度 并实时做短信验证,代码如下

[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) {

///执行相对应的操作及网络请求

}

}

上一篇:easyui中对于dialog页面传值的接收


下一篇:教你轻轻松松入门PHP