UIAlertController
//IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent*)event{
//preferredStyle:样式两种。
//一种是:alertView类型。从中间弹出
//一种actionsheet类型。从下面弹出
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"请输入密码" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//相当于我们alertView中的按钮系统绑定了一个blok块(对应的按钮的触发事件)
UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"取消按钮被点击了");
}];
//按钮:没有索引没有代理了有Block可以响应事件
UIAlertAction * actionDel = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"这是要删除一条信息");
}];
//添加文本输入框actionsheet类型的不支持
//[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.secureTextEntry =YES;
}];
//添加行为按钮
[alert addAction:action];
[alert addAction:actionDel];
//模态展示方式
[self presentViewController:alert animated:YES completion:nil];
}
UIAlertView
//创建一个提示框
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"密码错误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定",nil];
//导入代理协议UIAlertViewDelegate。设置代理
//alert.delegate = self;初始化时已经写了代理
//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(@"点击了取消按钮");
break;
case1:
NSLog(@"点击了确定按钮");
break;
default:
break;
}
}
UIActionSheet
//UIActionSheet: 标题选项提示框
//initWithTitle:提示框的标题
//delegate:代理:self表示当前类的调用
//destructiveButtonTitle:选项功能按钮标题为红色字体,默认第0个选项是红色字体,可以修改,手动设置属性
//otherButtonTitles:其他按钮标题
//触发提示框的方法
-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"提示框标题" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"警示性选项标题" otherButtonTitles:@"第一个选项",@"第二个选项",nil];
//设置红色字体选项的索引
actionSheet.destructiveButtonIndex= 0;
//设置取消按钮的索引
actionSheet.cancelButtonIndex= 3;
//样式设置
actionSheet.actionSheetStyle=UIActionSheetStyleDefault;
//显示提示框
[actionSheetshowInView:self.view];
}
#pragma mark ===== actionSheet代理方法
-(void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
switch(buttonIndex) {
case0:
//警示选项
break;
case1:
//第一个选项
break;
case2:
//第二个选项
break;
case3:
//取消按钮
break;
default:
break;
}
}
//在点击选项按钮以后actionSheet将要消失的时候调用
-(void)actionSheet:(UIActionSheet*)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"willDismissWithButtonIndex ======%ld",(long)buttonIndex);
}
//在点击选项按钮以后actionSheet已经消失的时候调用
-(void)actionSheet:(UIActionSheet*)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
NSLog(@"didDismissWithButtonIndex===%ld",(long)buttonIndex);
}