本文转载至 http://blog.csdn.net/jinkaiouyang/article/details/35551769

IOS8中,Apple将UIActionSheet和UIAlertView整合成一个接口UIAlertController。

原来的是一个view,展示在window视图之上。现在改成了controller,展示方式变成由当前的controller直接present出来。

下面看看具体的接口:

  1. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"This is Title"
  2. message:@"This is message"
  3. preferredStyle:UIAlertControllerStyleAlert];
  4. [alert addAction:[UIAlertAction actionWithTitle:@"Action 1 (Default Style)"
  5. style:UIAlertActionStyleDefault
  6. handler:^(UIAlertAction *action) {
  7. NSLog(@"Action 1 Handler Called");
  8. }]];
  9. [alert addAction:[UIAlertAction actionWithTitle:@"Action 2 (Cancel Style)"
  10. style:UIAlertActionStyleCancel
  11. handler:^(UIAlertAction *action) {
  12. NSLog(@"Action 2 Handler Called");
  13. }]];
  14. [alert addAction:[UIAlertAction actionWithTitle:@"Action 3 (Destructive Style)"
  15. style:UIAlertActionStyleDestructive
  16. handler:^(UIAlertAction *action) {
  17. NSLog(@"Action 3 Handler Called");
  18. }]];
  19. [self presentViewController:alert animated:YES completion:nil];
  1. <span style="font-family:Arial, Helvetica, sans-serif;">生成UIAlertController的时候可以指定title、message,通过preferredStyle可以指定具体是Alert还是</span><span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 14px;">ActionSheet</span>
  1. typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {
  2. UIAlertControllerStyleActionSheet = 0,
  3. UIAlertControllerStyleAlert
  4. } NS_ENUM_AVAILABLE_IOS(8_0);

通过addAction接口添加具体按钮,设置按钮title、style和使用block方式直接加入按钮响应接口。

style有以下几种:

  1. typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
  2. UIAlertActionStyleDefault = 0,
  3. UIAlertActionStyleCancel,
  4. UIAlertActionStyleDestructive
  5. } NS_ENUM_AVAILABLE_IOS(8_0);

与原来的UIActionSheet和UIAlertView比较:

1、视觉上没有改变,跟之前的样式保持一致;

2、改为controller方式后,控件的生命周期能够更好的控制;方便的弹出和收起

3、当前controller弹出UIAlertController后,再次present controller必须在UIAlertController的基础上present。present后,UIAlertController会被盖住。而之前的UIActionSheet和UIAlertView由于是add在window上面的,当在弹出他们的controller基础上再present controller的话,UIActionSheet和UIAlertView弹框仍然保留在最上方。

4、修改为controller方式后,接口更加简单,而且按钮响应方式更加明了简介,不需要在设置delegate和实现响应的回调等。

最新文章

  1. zlog学习笔记(level_list)
  2. iOS - Swift 基本语法
  3. 基于jQuery的上下左右无缝滚动应用(单行或多行)
  4. IOS开发之UINavigationController详解
  5. Android - 传统蓝牙(蓝牙2.0)
  6. 【机器学习】--LDA初始和应用
  7. JS高级
  8. IBase&lt;T&gt;
  9. 2017-12-15python全栈9期第二天第五节之while else的用法二当不被break打断时else内容的结果会被打印
  10. day 34 编程之补充内容
  11. 用VsCode写Markdown
  12. dom实例,模态框,全选,反选,取消
  13. mysql Lock wait timeout exceeded; try restarting transaction解决
  14. Codeforces Round #371 (Div. 2) D. Searching Rectangles 交互题 二分
  15. ASP.NET 的ClientIDMode属性
  16. Java 8 – Convert Instant to ZonedDateTime
  17. cocoa 的大招(KVC的几点强大应用记录)
  18. android自定义控件的一个思路-入门
  19. 2243: [SDOI2011]染色 树链剖分+线段树染色
  20. 编程开发之--java多线程学习总结(3)类锁

热门文章

  1. C#连接数据库SQL(2005)
  2. 【kindeditor】kindeditor的使用
  3. 为IIS增加PHP支持
  4. 使用Python计算研究生学分绩(绩点)
  5. babel ---- presets字段设定转码规则
  6. R语言集合函数
  7. 遇到的cannot find module &#39;xxx&#39; 问题
  8. 一次完整的http请求过程以及网络I/O模型select、epoll
  9. Chelly的串串专题
  10. Java---详解方法传值问题