首先说下UIAlertView和UIActionSheet在iOS9之后苹果官方就不推荐使用了,而是使用UIAlertController来替代。
有图为证
所以学习一下UIAlertController的用法也是有必要的。
1.获取UIAlertController的类对象:
UIAlertController *alertController =[UIAlertController alertControllerWithTitle:@"Success" message:nil preferredStyle:UIAlertControllerStyleAlert];
2.获取UIAlertAction的类对象:
UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"Done" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"123");
}];
3.用UIAlertController的类对象alertController的addAction方法添加UIAlertAction的类对象:
[alertController addAction:doneAction];
4.使用window的根视图模态出alertController
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
如果想用UIActionSheet类似的界面,只需要将第一步中的UIAlertControllerStyleAlert替换成UIAlertControllerStyleActionSheet即可。
如果程序用了这个API,那么在iOS7上会导致crash,所以需要版本兼容
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的频率越来越高,或许这也代表了一种趋势
荒野乱斗国际版 (BrawlStars)最新版本v56.274
下载一波超人内置修改器菜单版 安卓版v1.0.2
下载敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载继承了一座戏园子无限声望铜钱版 内置菜单最新版v1.7
继承了一座戏园子折相思版是游戏的破解版本,在该版本中为玩家提
山河半世橙光清软金手指版 无限鲜花v3.24
山河半世是一款超级好玩的橙光恋爱游戏,在游戏中玩家们需要扮演
蓬莱手游折相思版 安卓版v1.0.0
蓬莱免广告版是游戏的修改版本,在该版本中为玩家去除了广告,玩
当红影后橙光游戏破解版2025 最新版v1.0
当红影后橙光破解版是一款超级好玩的娱乐圈题材的橙光游戏,在这
忽然成了万人迷清软完结版 无限鲜花版v12.15
忽然成了万人迷破解版是一款非常好玩的男性向橙光游戏,在有一天