UIAlertController
//IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
//preferredStyle:樣式兩種。
//一種是:alertView類型。從中間彈出
//一種actionsheet類型。從下面彈出
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"請(qǐng)輸入密碼" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//相當(dāng)于我們alertView中的按鈕系統(tǒng)綁定了一個(gè)blok塊(對(duì)應(yīng)的按鈕的觸發(fā)事件)
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消按鈕被點(diǎn)擊了");
}];
//按鈕:沒有索引沒有代理了有Block可以響應(yīng)事件
UIAlertAction * actionDel = [UIAlertAction actionWithTitle:@"刪除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"這是要?jiǎng)h除一條信息");
}];
//添加文本輸入框actionsheet類型的不支持
//[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.secureTextEntry =YES;
}];
//添加行為按鈕
[alert addAction:action];
[alert addAction:actionDel];
//模態(tài)展示方式
[self presentViewController:alert? animated:YES completion:nil];
}
UIAlertView
提示框?
//創(chuàng)建一個(gè)提示框
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"密碼錯(cuò)誤" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定",nil];
//導(dǎo)入代理協(xié)議UIAlertViewDelegate。設(shè)置代理
//alert.delegate = self;初始化時(shí)已經(jīng)寫了代理
//show一下展示提示框
[alert show];
#pragma mark == UIAlertViewDelegate方法
//-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
//-(void)alertViewCancel:(UIAlertView *)alertView
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//條件判斷語句
switch(buttonIndex) {
case0:
NSLog(@"點(diǎn)擊了取消按鈕");
break;
case1:
NSLog(@"點(diǎn)擊了確定按鈕");
break;
default:
break;
}
}
UIActionSheet
//UIActionSheet: 標(biāo)題選項(xiàng)提示框
//initWithTitle:提示框的標(biāo)題
//delegate:代理:self表示當(dāng)前類的調(diào)用
//destructiveButtonTitle:選項(xiàng)功能按鈕標(biāo)題為紅色字體,默認(rèn)第0個(gè)選項(xiàng)是紅色字體,可以修改,手動(dòng)設(shè)置屬性
//otherButtonTitles:其他按鈕標(biāo)題
//觸發(fā)提示框的方法
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"提示框標(biāo)題" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"警示性選項(xiàng)標(biāo)題" otherButtonTitles:@"第一個(gè)選項(xiàng)",@"第二個(gè)選項(xiàng)",nil];
//設(shè)置紅色字體選項(xiàng)的索引
actionSheet.destructiveButtonIndex= 0;
//設(shè)置取消按鈕的索引
actionSheet.cancelButtonIndex= 3;
//樣式設(shè)置
actionSheet.actionSheetStyle=UIActionSheetStyleDefault;
//顯示提示框
[actionSheetshowInView:self.view];
}
#pragma mark ===== actionSheet代理方法
-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch(buttonIndex) {
case0:
//警示選項(xiàng)
break;
case1:
//第一個(gè)選項(xiàng)
break;
case2:
//第二個(gè)選項(xiàng)
break;
case3:
//取消按鈕
break;
default:
break;
}
}
//在點(diǎn)擊選項(xiàng)按鈕以后actionSheet將要消失的時(shí)候調(diào)用
-(void)actionSheet:(UIActionSheet*)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"willDismissWithButtonIndex ======%ld",(long)buttonIndex);
}
//在點(diǎn)擊選項(xiàng)按鈕以后actionSheet已經(jīng)消失的時(shí)候調(diào)用
-(void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"didDismissWithButtonIndex===%ld",(long)buttonIndex);
}