iOS中UIAlertController控件的使用

在开发过程中我们平时使用的UIAlertView 提示的控件在ios9中已经被废弃使用了,而一直开发以来用惯了这个控件的我尽管有些不舍,但是看着出现的警告,有强迫症的我是不太能接受的,转用了最新的控件UIAlertController。

 UIAlertController首先他是继承于UIViewController,而UIAlertView是继承于UIView的,从这点可以看出他们两个的区别所在,而且很强大的是它将UIAlertControllerStyleActionSheet和UIAlertControllerStyleAlert收于它之下,也没有什么代理啊等方法,废话不说,直接上代码演示。

```

UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"私信TA" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"私信TA");

}];

```

UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"不再看该内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"不再看该内容");

}];

UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"举报该内容" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

NSLog(@"举报该内容");

}];

UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {

}];

UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];//这里是警告类型

[alertView addAction:action1];

[alertView addAction:action2];

[alertView addAction:action3];

[alertView addAction:action4];

[self presentViewController:alertView animated:YES completion:nil];

效果图1:


图1

如果将上面改成表单的话UIAlertControllerStyleActionSheet

效果图2:


图2

你要做一些操作的话可以在action的block里面做,再也不用写代理方法了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容