UIAlertController的使用
2015-11-18 17:56
671 查看
从iOS8开始,UIAlertView和UIActionSheet都不再推荐了,应该开始使用一个新的API,UIAlertController
UIAlertController继承自UIViewController,所以显示的方法不是show,而是普通的present。另外,如果程序用了这个API,那么在iOS7上会导致crash,所以需要版本兼容
Objc代码 收藏代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[mainViewDelegate doBackupWithTag:BACKUP_STATISTICS_TAG_CONFIRM];
}];
[alert addAction:cancel];
[alert addAction:confirm];
[self presentViewController:alert animated:YES completion:nil];
可以看到,最大的区别,是UIAlertController不再使用delegate的方式来触发回调,而是直接传一个block
delegate和block并没有本质区别,只是触发回调的不同方式而已,解决的都是“在未来的某个时间,调用我”的问题。delegate的复用性更好一点,创建一个delegate实例之后,可以把它设置为多个控件的delegate,减少了重复。block的优势是更加直观,阅读起来更容易,因为代码都在一处,不需要跳来跳去地读代码
但是现在既然苹果官方使用block的频率越来越高,或许这也代表了一种趋势
UIAlertController继承自UIViewController,所以显示的方法不是show,而是普通的present。另外,如果程序用了这个API,那么在iOS7上会导致crash,所以需要版本兼容
Objc代码 收藏代码
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"title" message:@"message" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[mainViewDelegate doBackupWithTag:BACKUP_STATISTICS_TAG_CONFIRM];
}];
[alert addAction:cancel];
[alert addAction:confirm];
[self presentViewController:alert animated:YES completion:nil];
可以看到,最大的区别,是UIAlertController不再使用delegate的方式来触发回调,而是直接传一个block
delegate和block并没有本质区别,只是触发回调的不同方式而已,解决的都是“在未来的某个时间,调用我”的问题。delegate的复用性更好一点,创建一个delegate实例之后,可以把它设置为多个控件的delegate,减少了重复。block的优势是更加直观,阅读起来更容易,因为代码都在一处,不需要跳来跳去地读代码
但是现在既然苹果官方使用block的频率越来越高,或许这也代表了一种趋势
相关文章推荐
- 解决github push错误The requested URL returned error: 403 Forbidden while accessing
- UITouch 与 手势识别器
- FreeMarkerConfigurer使用TemplateDirectiveModel时获取request、session
- QOS
- UITableView样式的自定义
- ios UIButton点击事件(@selector)注意事项
- - (void)layoutSublayersOfLayer:(CALayer *)layer
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
- UICopyLable
- UICollectionView UITableView registerNib和registerClass的回调
- String Stringbuffer Stringbuilder
- 06 OSPF路由器协议
- UE4 Console Commands List
- 04 RIP路由协议
- 文章标题
- IOS控件学习:UILabel常用属性与用法
- 获取请求信息——Request对象的方法
- NSDate和UIImage 数据相互转化_数据库中只能存二进制数据
- 近期UI设计经验回顾
- iOS通过URL构建UIImage