方法1:
(1)给TextField添加和Button一样的方法,forControlEvents选择UIControlEventEditingChanged;
(2)执行方法;
如下图 : 限制最大长度为6字符,每输入一个字符就打印一次,当输到第6个或超过第6个的时候,就只截取前6位字符.
方法2:
(1)UITextField签订协议
(2)利用系统的方法,黄色框里的是想用哪个TextField上.
附上上图代码:
#pragma mark - UITextFieldDelegate限制最多输入6个字符串
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
if(textField ==self.smsField||textField ==self.setNewPasswordField||textField ==self.confirmPasswordField) {
if(string.length==0)returnYES;
NSIntegerexistedLength = textField.text.length;
NSIntegerselectedLength = range.length;
NSIntegerreplaceLength = string.length;
if(existedLength - selectedLength + replaceLength >6) {
returnNO;
}
}
returnYES;
}