UIAlertView 与 UIActionSheet

  • UIAlertView

    • 样式

    • 实现

      - (void)showAlertView {
      self.alertView = [[UIAlertView alloc] initWithTitle:@"确定操作吗?" message:@"确定可能会有灾难哦!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
      self.alertView.delegate = self;
      [self.alertView show];
      }
    • 注意

      • 其“确定”按钮的颜色与“取消”按钮的外观一样(没有显示红色,即 normal)
  • UIActionSheet
    • 样式

    • 实现

      - (void)showActionSheet {
      self.actionSheet = [[UIActionSheet alloc] initWithTitle:@"确定操作吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
      [self.actionSheet showInView:self.view];
      }
    • 注意

      • 其“确定”按钮的颜色与“取消”按钮的外观不一样(显示红色,即 destructive)

UIAlertController

  • 概述

    • UIAlertController出现的原因,我想就不必多说了。来看看苹果官方的介绍吧!

        A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method UIAlertController 
      
        实例是用来向用户警告信息的。该类旨在替代 UIActionSheet 和 UIAlertView。若你按照自己的需求配置了 UIAlertController 的 actions 和 style,就使用 presentViewController:animated:completion: 方法来显示它吧
  • UIAlertController 的使用步骤

    • 初始化 UIAlertController, 并设置标题,副标题,alert的样式(alert 或 actionSheet)
    • 添加事件
      • 使用 UIAlertAction 定义每一个事件,与事件相关的 title、style、action
      • style
        • UIAlertActionStyleDefault
        • UIAlertActionStyleCancel
        • UIAlertActionStyleDestructive
      • action
        • 使用 block 代替了原来的 代理模式
    • 显示 alert
  • UIAlertController 的简单使用

    • 显示 alertView

      • 初始化 UIAlertController

        self.alertController = [UIAlertController alertControllerWithTitle:@"确定操作吗?" message:@"确定可能会有灾难哦!" preferredStyle:UIAlertControllerStyleAlert];
      • 添加事件(事件的添加顺序,会影响按钮的显示顺序

        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //TODO:
        }];
        [self.alertController addAction:cancelAction];
        UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //TODO:
        }];
        [self.alertController addAction:confirmAction];
      • 显示 alert

        [self presentViewController:self.alertController animated:YES completion:^{
        // TODO
        }];
    • 显示 actionSheet

      • 在此就不再贴代码了,把 上述代码的 UIAlertControllerStyleAlert 改成 UIAlertControllerStyleActionSheet 试试吧

UIAlertController 中的 textField

  • 注意

    • 只能向 alert 类型的 UIAlertController 中添加 textField

    • 向 actionSheet 类型的 UIAlertController 中添加 textField,会报运行时错误

      Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert

  • 使用 UIAlertController 实现登录界面(demo
    • 效果

    • 具体实现

      • 使用 CocoaPods 集成 MBProgressHUD 框架(CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)

        • 编辑 podfile 文件,如下:

        • 为了使用的方便,通常会为 MBProgressHUD 添加分类,在此只添加 showMessage 方法,如下

          + (void)showMessage:(NSString *)message {
          // hud 显示的 view
          UIView *contentView = [[UIApplication sharedApplication].windows lastObject];
          // hud
          MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:contentView animated:YES];
          // hud 显示的信息
          hud.detailsLabelText = message;
          // 当 hud 隐藏时是否从父控件中移除
          hud.removeFromSuperViewOnHide = YES;
          // hub 显示的时间
          [hud hide:YES afterDelay:1.5f];
          }
      • 设置 UIAlertController
        • 初始化 UIAlertController

          self.alertController = [UIAlertController alertControllerWithTitle:@"登录" message:nil preferredStyle:UIAlertControllerStyleAlert];
        • 添加 textField(textField 与 action 的添加顺序,不影响其显示顺序)

          [self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
          textField.placeholder = @"账户";
          }];
          [self.alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
          textField.placeholder = @"密码";
          }];
        • 添加事件

          UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
          // TODO:
          }];
          [self.alertController addAction:cancelAction];
          UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
          // get the account and password
          UITextField *accountTextField = self.alertController.textFields[0];
          UITextField *passwordTextField = self.alertController.textFields[1];
          NSString *message = [NSString stringWithFormat:@"账户:%@\n密码:%@", accountTextField.text, passwordTextField.text];
          // 显示 MBProgressHUD(需要在主线程中显示)
          dispatch_async(dispatch_get_main_queue(), ^{
          [MBProgressHUD showMessage:message];
          });
          }];
          [self.alertController addAction:confirmAction];
        • 显示 alert

          [self presentViewController:self.alertController animated:YES completion:^{
          // TODO
          }];

开始使用 UIAlertController 吧的更多相关文章

  1. UIAlertController

    楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...

  2. iOS UIAlertController跟AlertView用法一样 && otherButtonTitles:(nullable NSString *)otherButtonTitles, ... 写法

    今天写弹出框UIAlertController,用alertView习惯了,所以封装了一下,跟alertView用法一样,不说了,直接上代码: 先来了解一下otherButtonTitles:(nul ...

  3. UIAlertController使用

    // 将UIAlertController模态出来 相当于UIAlertView show 的方法// 初始化一个一个UIAlertController    // 参数preferredStyle: ...

  4. IOS UIAlertController 使用方法

    在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, ...

  5. UI控件(UIAlertController)

    @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; UIButton *_button = [UIBut ...

  6. UIAlertController 部分用法及属性

    //创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...

  7. iOS--UIAlertView与UIAlertController和UIAlertAction之间的事儿

      iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备 ...

  8. UI第十四节——UIAlertController

    - (void)viewDidLoad {    [super viewDidLoad];        UIButton *alertBtn = [UIButton buttonWithType:U ...

  9. iOS 8.0后使用UIAlertController

    iOS 8的新特性之一就是让接口更有适应性.更灵活,因此许多视图控制器的实现方式发生了巨大的变化.全新的UIPresentationController在实现视图控制器间的过渡动画效果和自适应设备尺寸 ...

随机推荐

  1. CDH安装失败了,如何重新安装

    1> 删除Agent节点的UUID # rm -rf /opt/cm-5.4.7/lib/cloudera-scm-agent/* 2>  清空主节点CM数据库 进入主节点的Mysql数据 ...

  2. mysql悲观锁总结和实践--转

    原文地址:http://chenzhou123520.iteye.com/blog/1860954 最近学习了一下数据库的悲观锁和乐观锁,根据自己的理解和网上参考资料总结如下: 悲观锁介绍(百科): ...

  3. CSS3的flex布局

    flex的一些属性 CSS3中引入了另一种框--flexbox,flexbox有一些block和inline不同的性质,比如: 自适应子元素(flex item,又称伸缩项目)的宽度 伸缩项目的flo ...

  4. 【Java心得总结六】Java容器中——Collection

    在[Java心得总结五]Java容器上——容器初探这篇博文中,我对Java容器类库从一个整体的偏向于宏观的角度初步认识了Java容器类库.而在这篇博文中,我想着重对容器类库中的Collection容器 ...

  5. 移动端用js与jquery实时监听输入框值的改动

    背景: 在一次移动端H5开发中,需要监听输入框值的实时变动. onchange事件肯定抛弃,因为只能失去焦点才触发. 而keyPress在Android可以触发,iOS不可以. 又不想用Android ...

  6. MSIL Hello World

    最近由于需要,开始阅读 MSIL 方面的东西.我读的是<.NET 探秘——MSIL 权威指南>(<Expert .NET 2.0 IL Assembler>中译版).感觉没什么 ...

  7. ODBC database driver for Go:Go语言通过ODBC 访问SQL server

    Go语言通过ODBC 访问SQL server,这里需要用到go-odbc库,开源地址::https://github.com/weigj/go-odbc 一.驱动安装 在cmd中打开GOPATH: ...

  8. JAVAWEB项目实现验证码中文、英文、数字组合

    验证码基础 一.什么是验证码及它的作用 :验证码为全自动区分计算机和人类的图灵测试的缩写,是一种区分用户是计算机的公共全自动程序,这个问题可以由计算机生成并评判,但是必须只有人类才能解答.可以防止恶意 ...

  9. 对jquery的ajax进行二次封装以及ajax缓存代理组件:AjaxCache

    虽然jquery的较新的api已经很好用了, 但是在实际工作还是有做二次封装的必要,好处有:1,二次封装后的API更加简洁,更符合个人的使用习惯:2,可以对ajax操作做一些统一处理,比如追加随机数或 ...

  10. 在MVC控制器里面使用dynamic和ExpandoObject,实现数据转义的输出

    在很多时候,我们在数据库里面定义表字段和实际在页面中展示的内容,往往是不太匹配的,页面数据可能是多个表数据的综合体,因此除了我们在表设计的时候考虑周到外,还需要考虑数据展现的处理.如果是常规的处理,那 ...