UIAlertController的用法以及和其与UIAlertView的区别

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]

    ;


其运行效果是:UIAlertController的用法以及和其与UIAlertView的区别_第1张图片

修改otherAction的样式:UIAlertActionStyleDestructive 其运行效果为:UIAlertController的用法以及和其与UIAlertView的区别_第2张图片

修改UIAlertController的样式:UIAlertControllerStyleActionSheet其运行效果:UIAlertController的用法以及和其与UIAlertView的区别_第3张图片



如果有多个按钮可通过增加:UIAlertAction来达到想要的效果





你可能感兴趣的:(iOS遇到的那些事)