1.前段时间iOS升级后突然发现UIAlertView居然弃用啦,最后上网搜下才发现出现了衍生出新的控件UIAlertController,好吧!刚开始使用发现真的很不习惯,不过用一段时间也就习惯啦,这段时间使用发现UIAlertController相对于UIAlerView其实就是一下几点不同:
1.弃用了代理开始使用代码块block,这里一定要注意循环引用的问题
2.设置按钮开始分开设置,不再像以前一样一次设置完成啦!
其他的不同,我也没怎么发现!
言归正传,上代码说说用法:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"我是标题" message:@"我是提示信息" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消执行");
}];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定1" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定执行");
}];
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil]
;
其运行效果是:
修改otherAction的样式:UIAlertActionStyleDestructive 其运行效果为:
修改UIAlertController的样式:UIAlertControllerStyleActionSheet其运行效果:
如果有多个按钮可通过增加:UIAlertAction来达到想要的效果