在IOS 9.0 后 苹果官方宣布不再或不推荐使用UIAlertView 和 UIActionSheet 由UIAlertController进行代替两者 用控制器将两者合二为一 很简单 方便 下面就是关于UIAlertView的常用方法
一)新旧对比:
标准的Alert样式:
旧方法:UIAlertView:
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"主标题" message:@"提示文字"
delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//显示alertView
[alertView show];
新方法:UIAlertController:
//UIAlertController风格:UIAlertControllerStyleAlert
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"主标题" message:@"提示文字"preferredStyle:UIAlertControllerStyleAlert ];
//添加取消到UIAlertController中
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
//添加确定到UIAlertController中
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil]
标准的Alert Sheet样式:
旧方法:UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"标准的Action Sheet样式" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"了解更多"otherButtonTitles:@"原来如此", nil];
[actionSheet showInView:self.view];
新方法:UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标准的Action Sheet样式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//取消:style:UIAlertActionStyleCancel//
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertController addAction:cancelAction];
//了解更多:style:UIAlertActionStyleDestructive
UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:moreAction];
//原来如此:style:UIAlertActionStyleDefault
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原来如此" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
二)新功能:
UIAlertController 并不只是对已有的 API 做了清理,而是进行了标准化归纳。以前,预设的样式闲置有很多(swizzling 虽然可以提供更多的功能但还是有很大风险)。UIAlertController 让以前看起来很神奇的事情变为了可能。
这种行为已经被UIAlertActionStyle所覆盖,共有三种类型:
style:UIAlertActionStyleDefault//对按钮应用标准样式
style:UIAlertActionStyleCancel//对按钮应用取消样式,即取消操作
style:UIAlertActionStyleDestructive//对按钮应用警示性样式,提示用户这样做可能会删除或者改变某些数据
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"主标题:标题党"message:@"子标题:提示信息" preferredStyle:UIAlertControllerStyleAlert ];
//取消style:UIAlertActionStyleDefault
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
//style:UIAlertActionStyleDestructive(警告提示)
UIAlertAction *rubbishAction = [UIAlertAction actionWithTitle:@"确定修改" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:rubbishAction];
[self presentViewController:alertController animated:YES completion:nil];
三)弹出文本框
// 只有在alert情况下才可以添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"用户名";
textField.secureTextEntry = YES;
}];
// // 取出文本
// UITextField *text = alertController.textFields.firstObject;
// UIAlertAction *action = alertController.actions.firstObject;