zoukankan      html  css  js  c++  java
  • IOS开发之IOS8.0最新UIAlertController 分类: ios技术 2015-01-20 14:24 144人阅读 评论(1) 收藏

    最近苹果更新的IOS8 对以前进行了很大的修改, 更新的API也让人捉急,据说iOS 8的新特性之一就是让接口更有适应性、更灵活,因此许多视图控制器的实现方式发生了巨大的变化。比如全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸变化效果(比如说旋转)中发挥了重要的作用,它有效地节省了程序员们的工作量。但是同时某些旧的UIKit控件也同样发生了许多变化,很多自定义在旧控件上的控件发生了诡异的BUG,其实UIAlertView、UIActionSheet (以及它们各自的 delegate protocols) 在 iOS 8 中已经被废弃了,在你的代码中按住QQ图片20141219102558.png点击 UIAlertView 或者 UIActionSheet,你就会看到最上面的注释:UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.虽然类已经被废弃了,但在 @availability 属性中并没有表达出这一点。UIAlertView 目前还是能用的。更新不可怕,学会了就可以拿来用了.本文将会对Alert Views和Action Sheets发生的改变进行一个大致的介绍.



    UIAlertView

    随着苹果上次iOS 5的发布,UIAlertView 充满了无底线的让步,牺牲格式和设计正确性来顺应开发者的喜好。UIAlertView警告框就一直被大量使用,简单易用,应用场景巨大,一般使用时候常伴随两个按键,确定&取消.比如:

    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"标题" message:@"这个是UIAlertView的默认样式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
    [alertview show];
    您也可以通过更改UIAlertView的alertViewStyle属性来实现输入文字、密码甚至登录框的效果。UIAlertViewDelegate协议拥有响应对话框视图的按钮动作的回调方法。还有当文本框内容改变时,调用alertViewShouldEnableOtherButton:方法可以让按钮动态地可用或者不可用。

    说了这么多其实都没什么用,因为在IOS8.0之后 ,苹果已经不建议我们用UIAlertView了,而是新的UIAlertController 


    UIAlertController


    UIAlertController 同时替代了 UIAlertView 和 UIActionSheet,从系统层级上统一了 alert 的概念 —— 即以 modal 方式或 popover 方式展示。

    UIAlertController 是 UIViewController 的子类,而非其先前的方式。因此新的 alert 可以由 view controller 展示相关的配置中获益很多。

    UIAlertController 不管是要用 alert 还是 action sheet 方式展示,都要以 title 和 message 参数来初始化。Alert 会在当前显示的 view controller 中心以模态形式出现,action sheet 则会在底部滑出。Alert 可以同时有按钮和输入框,action sheet 仅支持按钮。

    新的方式并没有把所有的 alert 按钮配置都放在初始化函数中,而是引入了一个新类 UIAlertAction 的对象,在初始化之后可以进行配置。这种形式的 API 重构让对按钮数量、类型、顺序方便有了更大的控制。同时也弃用了 UIAlertView 和 UIActionSheet 使用的delegate 这种方式,而是采用更简便的完成时回调。

    <span style="color:#33cc00;"><strong>// 注意这里的</strong></span><span style="font-family: 'Helvetica Neue', Helvetica, STheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;"><span style="color:#33cc00;"><strong>preferredStyle的参数是一个枚举值 <span style="color: rgb(51, 204, 0); font-family: Menlo;font-size:18px; line-height: 28px; white-space: pre; background-color: rgb(240, 240, 240);">UIAlertControllerStyleActionSheet 就是原UIActionSheet</span></strong></span></span><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">typedef NS_ENUM(NSInteger, UIAlertControllerStyle) {</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">    UIAlertControllerStyleActionSheet = 0,</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">    UIAlertControllerStyleAlert</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-family: Menlo;"><span style="color:#33cc00;">} NS_ENUM_AVAILABLE_IOS(8_0);</span></p>
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"<span style="font-family: 'Helvetica Neue', Helvetica, STheiti, 微软雅黑, 黑体, Arial, Tahoma, sans-serif, serif;">message</span>" preferredStyle:UIAlertControllerStyleAlert]; 
    <pre name="code" class="objc"><span style="color:#33cc00;"><strong>// 注意这里的style参数是按钮样式,也是枚举值分别为:</strong></span>
    </pre><pre name="code" class="objc" style="color: rgb(255, 102, 0);">UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:sureAction];


    
    还没有完事,因为现在警告框已经从一个View"进化"为一个Controller,所以显示方式也不再是show或者showIn
     ,而是
    [self presentViewController:self.alertController animated:YES completion:nil];
    注意,在试验时一定不要放在
    -(void)viewDidLoad

    方法里,(提示一下)可以写在视图出现或者自定义一个Button的触发方法里.






    viewDidLoad

    viewDidLoad



    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    记录一次在 VirtualBox的添加共享windows文件后,发现没有共享文件的事
    linux 压缩解压缩命令
    关于erlang中的timer:tc/3
    python基础:while循环,for循环
    grep和正则表达式参数
    grep和正则表达式
    nginx反向代理三台web
    安装nginx包
    部署samba
    samba了解
  • 原文地址:https://www.cnblogs.com/liuqixu/p/4684011.html
Copyright © 2011-2022 走看看