- (
BOOL
)textField:(
UITextField
*)textField shouldChangeCharactersInRange:(
NSRange
)range replacementString:(
NSString
*)string;
{ //string 就是此时输入的那个字符 textField 就是此时正在输入的那个输入框返回 YES 就是可以改变输入框的值 NO 相反
if ([string isEqualToString : @"\n" ]) // 按会车可以改变
{
return YES ;
}
NSString * toBeString = [textField. text stringByReplacingCharactersInRange :range withString :string]; // 得到输入框的内容
if ( self . PassTextField == textField) // 判断是否时我们想要限定的那个输入框
{
if ([toBeString length ] > 6 ) { // 如果输入框内容大于 20 则弹出警告
textField. text = [toBeString substringToIndex : 5 ];
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : nil
message : @" 超过最大字数不能输入了 "
delegate : nil
cancelButtonTitle : @"Ok"
otherButtonTitles : nil , nil ];
[alert show ];
return NO ;
}
}
return YES ;
{ //string 就是此时输入的那个字符 textField 就是此时正在输入的那个输入框返回 YES 就是可以改变输入框的值 NO 相反
if ([string isEqualToString : @"\n" ]) // 按会车可以改变
{
return YES ;
}
NSString * toBeString = [textField. text stringByReplacingCharactersInRange :range withString :string]; // 得到输入框的内容
if ( self . PassTextField == textField) // 判断是否时我们想要限定的那个输入框
{
if ([toBeString length ] > 6 ) { // 如果输入框内容大于 20 则弹出警告
textField. text = [toBeString substringToIndex : 5 ];
UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : nil
message : @" 超过最大字数不能输入了 "
delegate : nil
cancelButtonTitle : @"Ok"
otherButtonTitles : nil , nil ];
[alert show ];
return NO ;
}
}
return YES ;
}