(1)点确定无操作
UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"时间8:00 - 18:00" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
UIAlertAction *actions = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
}];
[alert1 addAction:actions];
[self presentViewController:alert1 animated:YES completion:nil];
(2)点确定有操作
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请开启视频" message:nil preferredStyle:UIAlertControllerStyleAlert];
//如果是取消 就选取消style
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//如果确定 且有动作 就在handler 里进行block操作
UIAlertAction *determine = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
ViewController *vc = [[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}];
//添加取消和确定动作
[alert addAction:cancel];
[alert addAction:determine];
//模态弹出
[self presentViewController:alert animated:YES completion:nil];
(3)有输入框警告
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请输入姓名" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"输入姓名";
textField.secureTextEntry = NO;
}];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *text = alert.textFields.firstObject;
if (text.text.length==0)
{
MBProgressHUD *hud = [Utils createHUD];
[hud hide:YES afterDelay:5];
_namelabel.text=@"必填";
_namelabel.textColor = [UIColor lightGrayColor];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"输入的名字不能为空";
}else{
_name=_namelabel.text=text.text;
_namelabel.textColor = [UIColor themeBlackColor];
}
}];
UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:no];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
(4)弹出有textField的 且里面有HUD的
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请输入患者身份证号码" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"输入身份证号码";
textField.keyboardType=UIKeyboardTypeEmailAddress;
//输入需要带******号吗
textField.secureTextEntry = NO;
//
}];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *text = alert.textFields.firstObject;
// 号码为空 后者少于18位
if (text.text.length==0||text.text.length<18)
{
_idnum=_idnumlabel.text=@"选填";
_idnumlabel.textColor = [UIColor lightGrayColor];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"您输入号码有误";
[hud hide:YES afterDelay:1.5];
});
return ;
}
//如果18位 但是不正确或准确
if (text.text.length==18)
{
//如果号码 是18位且满足验证
if ([self checkUserIdCard:text.text])
{
_idnum=_idnumlabel.text=text.text;
_idnumlabel.textColor = [UIColor themeBlackColor];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"一定要核实准确哦~~";
[hud hide:YES afterDelay:1.5];
});
return ;
}else
{
_idnum=_idnumlabel.text=@"选填";
_idnumlabel.textColor = [UIColor lightGrayColor];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"该身份证账号不存在";
[hud hide:YES afterDelay:1.5];
});
return ;
}
}
}];
UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:no];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
判断手机号的
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"请输入患者手机号码" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"输入手机号码";
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.secureTextEntry = NO;
}];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *text = alert.textFields.firstObject;
// 号码为空 后者少于11位
if (text.text.length==0||text.text.length<11)
{
_mobile= _mobilelabel.text=@"选填";
_mobilelabel.textColor = [UIColor lightGrayColor];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"您输入号码有误";
[hud hide:YES afterDelay:1.5];
});
return ;
}
//如果是11位
if (text.text.length==11)
{
//如果满足
if ([self validateMobile:text.text])
{
_mobile= _mobilelabel.text=text.text;
_mobilelabel.textColor = [UIColor themeBlackColor];
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"一定要核实准确哦~~";
[hud hide:YES afterDelay:1.5];
});
return ;
}else{
_mobile= _mobilelabel.text=@"选填";
_mobilelabel.textColor = [UIColor lightGrayColor];
// 显示 MBProgressHUD(需要在主线程中显示)
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [Utils createHUD1];
hud.mode = MBProgressHUDModeText;
hud.labelText = @"该手机账号不存在";
[hud hide:YES afterDelay:1.5];
});
return ;
}
}
}];
UIAlertAction *no = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:no];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
//判断手机号正则
- (BOOL) validateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//判断身份证号正则
#pragma 正则匹配用户身份证号18位
- (BOOL)checkUserIdCard: (NSString *) identityCard
{
BOOL flag;
if (identityCard.length <= 0)
{
flag = NO;
return flag;
}
NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
flag = [identityCardPredicate evaluateWithObject:identityCard];
//如果通过该验证,说明身份证格式正确,但准确性还需计算
if(flag)
{
if(identityCard.length==18)
{
//将前17位加权因子保存在数组里
NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//这是除以11后,可能产生的11位余数、验证码,也保存成数组
NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
//用来保存前17位各自乖以加权因子后的总和
NSInteger idCardWiSum = 0;
for(int i = 0;i < 17;i++)
{
NSInteger subStrIndex = [[identityCard substringWithRange:NSMakeRange(i, 1)] integerValue];
NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
idCardWiSum+= subStrIndex * idCardWiIndex;
}
//计算出校验码所在数组的位置
NSInteger idCardMod=idCardWiSum%11;
//得到最后一位身份证号码
NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2)
{
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
{
return flag;
}
else
{
flag = NO;
return flag;
}
}
}
else
{
flag = NO;
return flag;
}
}
else
{
return flag;
}
}