UIAlertController - 学习笔记

一个网管的iOS学习笔记,记录下自己这条路上的点点滴滴。都是一些很简单的笔记,不敢妄谈教学,纯粹只是为了记录自己在这条路上——前进着。

UIAlertController是iOS8推出的新概念,取代了之前的 UIAlertView和UIActionSheet(虽然现在仍可以使用,但是会有警告)。

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 这种方式,而是采用更简便的完成时回调。

正好上面这篇教程是swift的,所以我就写一下Objective-C的吧。

一)新旧对比:

标准的Alert样式:

《UIAlertController - 学习笔记》 标准的Alert样式

  • 旧方法:UIAlertView:
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"没有标题的标题"
                                                       message:@"学无止境,漫漫长路"
                                                      delegate:self
                                             cancelButtonTitle:@"取消"
                                             otherButtonTitles:@"确定", nil];
    //显示alertView
    [alertView show];
  • 新方法:UIAlertController:
    //UIAlertController风格:UIAlertControllerStyleAlert
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"没有标题的标题"
                                                                             message:@"学无止境,漫漫长路"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    
    //添加取消到UIAlertController中
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    //添加确定到UIAlertController中
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
标准的Alert Sheet样式:

《UIAlertController - 学习笔记》 标准的Alert Sheet样式

  • 旧方法:UIActionSheet
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"标准的Action Sheet样式"
                                                            delegate:self
                                                   cancelButtonTitle:@"取消"
                                              destructiveButtonTitle:@"了解更多"
                                                   otherButtonTitles:@"原来如此", nil];
    [actionSheet showInView:self.view];
  • 新方法:UIAlertController
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标准的Action Sheet样式"
                                                                             message:nil
                                                                      preferredStyle:UIAlertControllerStyleActionSheet];
    //取消:style:UIAlertActionStyleCancel
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    //了解更多:style:UIAlertActionStyleDestructive
    UIAlertAction *moreAction = [UIAlertAction actionWithTitle:@"了解更多" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:moreAction];
    //原来如此:style:UIAlertActionStyleDefault
    UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"原来如此" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:OKAction];

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

二)新功能:

UIAlertController 并不只是对已有的 API 做了清理,而是进行了标准化归纳。以前,预设的样式闲置有很多(swizzling 虽然可以提供更多的功能但还是有很大风险)。UIAlertController 让以前看起来很神奇的事情变为了可能。
ps:摘自——UIAlertController - NSHipster

带有警示按钮的 Alert:

《UIAlertController - 学习笔记》 带有警示按钮的 Alert

  • 这种行为已经被UIAlertActionStyle所覆盖,共有三种类型:
style:UIAlertActionStyleDefault//对按钮应用标准样式
style:UIAlertActionStyleCancel//对按钮应用取消样式,即取消操作
style:UIAlertActionStyleDestructive//对按钮应用警示性样式,提示用户这样做可能会删除或者改变某些数据
  • 所以想要对模态的 alert 加一个警示性的按钮,只需要加上 .Destructive 风格的 UIAlertAction 属性:
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这是标题啊标题-标题"
                                                                             message:@"这是消息啊消息-消息"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    //取消style:UIAlertActionStyleDefault
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    //简直废话:style:UIAlertActionStyleDestructive
    UIAlertAction *rubbishAction = [UIAlertAction actionWithTitle:@"简直废话" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:rubbishAction];

    [self presentViewController:alertController animated:YES completion:nil];
有很多个按钮的Alert:

《UIAlertController - 学习笔记》 有很多个按钮的Alert

  • 有 1 个或者 2 个操作的时候,按钮会水平排布。更多按钮的情况,就会像 action sheet 那样展示:
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"今天去谁家蹭饭呢"
                                                                             message:@"😄😄😄\n话说可以去好多好多妹纸家里蹭饭"
                                                                      preferredStyle:UIAlertControllerStyleAlert ];
    
    UIAlertAction *home1Action = [UIAlertAction actionWithTitle:@"去小红家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home1Action];
    UIAlertAction *home2Action = [UIAlertAction actionWithTitle:@"去小兰家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home2Action];
    UIAlertAction *home3Action = [UIAlertAction actionWithTitle:@"去小花家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home3Action];
    UIAlertAction *home4Action = [UIAlertAction actionWithTitle:@"去小娇家" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:home4Action];
    
    //取消style:UIAlertActionStyleDefault
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消(回家)" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];

还有“创建登录表单”和“创建注册表单”两块内容,基本上理解怎么实现,就放明天继续完善吧。睡觉!

    原文作者:degulade
    原文地址: https://www.jianshu.com/p/c4018f4fef0b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞