-(void)textFieldWithText:(UITextField *)textField{
UITextField * field = (UITextField*)textField;
switch (textField.tag) {
case ://发车单位/收车单位
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"unitname"]; //送达单位、收车单位
}
break;
case : //联系人
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"contact"];
}
break;
case ://联系电话
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"phone"];
}
break;
case ://详细地址
if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(, ) replacementString:@""]) {
[dic setObject:field.text forKey:@"address"];
}
break;
default:
break;
}
}
- (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]; //得到输入框的内容
switch (textField.tag) {
case ://发车单位/收车单位
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"单位输入不能超过20个字符"];
return NO;
}
break;
case : //联系人
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"联系人输入不能超过10个字符"];
return NO;
}
break;
case ://联系电话
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"联系电话输入不能超过11个字符"];
return NO;
}
break;
case ://详细地址
if ([toBeString length] > ) {
textField.text = [toBeString substringToIndex:];
[self createUIAlertController:@"详细地址输入不能超过30个字符"];
return NO;
}
break;
default:
break;
}
return YES;
}