(1)點(diǎn)確定無操作
UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"時(shí)間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)點(diǎn)確定有操作
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"請(qǐng)開啟視頻" message:nil preferredStyle:UIAlertControllerStyleAlert];
//如果是取消 就選取消style
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
//如果確定 且有動(dòng)作 就在handler 里進(jìn)行block操作
UIAlertAction *determine = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
ViewController *vc = [[ViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}];
//添加取消和確定動(dòng)作
[alert addAction:cancel];
[alert addAction:determine];
//模態(tài)彈出
[self presentViewController:alert animated:YES completion:nil];
(3)有輸入框警告
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請(qǐng)輸入姓名" 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:@"請(qǐng)輸入患者身份證號(hào)碼" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"輸入身份證號(hào)碼";
textField.keyboardType=UIKeyboardTypeEmailAddress;
//輸入需要帶******號(hào)嗎
textField.secureTextEntry = NO;
//
}];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *text = alert.textFields.firstObject;
// 號(hào)碼為空 后者少于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 = @"您輸入號(hào)碼有誤";
[hud hide:YES afterDelay:1.5];
});
return ;
}
//如果18位 但是不正確或準(zhǔn)確
if (text.text.length==18)
{
//如果號(hào)碼 是18位且滿足驗(yàn)證
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 = @"一定要核實(shí)準(zhǔn)確哦~~";
[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 = @"該身份證賬號(hào)不存在";
[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];
判斷手機(jī)號(hào)的
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"請(qǐng)輸入患者手機(jī)號(hào)碼" preferredStyle:UIAlertControllerStyleAlert];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"輸入手機(jī)號(hào)碼";
textField.keyboardType=UIKeyboardTypeNumberPad;
textField.secureTextEntry = NO;
}];
UIAlertAction *ok=[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *text = alert.textFields.firstObject;
// 號(hào)碼為空 后者少于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 = @"您輸入號(hào)碼有誤";
[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 = @"一定要核實(shí)準(zhǔn)確哦~~";
[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 = @"該手機(jī)賬號(hào)不存在";
[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];
//判斷手機(jī)號(hào)正則
- (BOOL) validateMobile:(NSString *)mobile
{
//手機(jī)號(hào)以13劫映, 15,18開頭,八個(gè) \d 數(shù)字字符
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];
}
//判斷身份證號(hào)正則
#pragma 正則匹配用戶身份證號(hào)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];
//如果通過該驗(yàn)證靶橱,說明身份證格式正確萨西,但準(zhǔn)確性還需計(jì)算
if(flag)
{
if(identityCard.length==18)
{
//將前17位加權(quán)因子保存在數(shù)組里
NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//這是除以11后,可能產(chǎn)生的11位余數(shù)、驗(yàn)證碼题山,也保存成數(shù)組
NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
//用來保存前17位各自乖以加權(quán)因子后的總和
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;
}
//計(jì)算出校驗(yàn)碼所在數(shù)組的位置
NSInteger idCardMod=idCardWiSum%11;
//得到最后一位身份證號(hào)碼
NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
//如果等于2坯临,則說明校驗(yàn)碼是10焊唬,身份證號(hào)碼最后一位應(yīng)該是X
if(idCardMod==2)
{
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
//用計(jì)算出的驗(yàn)證碼與最后一位身份證號(hào)碼匹配,如果一致看靠,說明通過求晶,否則是無效的身份證號(hào)碼
if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
{
return flag;
}
else
{
flag = NO;
return flag;
}
}
}
else
{
flag = NO;
return flag;
}
}
else
{
return flag;
}
}