- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; { if ([string isEqualToString:@"n"]) { return YES; } NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容 if (newPwd == textField || currPwd == textField)//判断是否时我们想要限定的那个输入框 { if ([toBeString length] > 20) { //如果输入框内容大于20则弹出警 textField.text = [toBeString substringToIndex:20]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"密码最多20位" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [alert show]; return NO; } } return YES; }