• iOS8新特性(1)——UIAlertController


    一、iOS8介绍

      iOS8 新特性,主要是UI上进行了统一

      1、UIAlertController
      2、UIPresentaionController:管理所有通过modal出来的控制器(看笔记)
      3、UIPopoverPresentationController
      4、SizeClass + Autolayout
      5、App Extension 应用扩展
      6、Core Image(iOS 5开始就有了,滤镜美图秀秀型 )
     
    二、UIAlertController
     
      1、以往我们使用的时候,都是用的 UIAlertView和UIActionSheet
     1 //选项弹出等
     2 -(void)alertViewOld
     3 {
     4     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"111" message:@"222" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
     5     alert.alertViewStyle = UIAlertViewStyleLoginAndPasswordInput;//4种方式
     6     [alert show];
     7     
     8     //如果要监听点击事件则需要去设置对应的代理,并在代理方法中操作
     9 }
    10 
    11 //底部弹出
    12 -(void)aletSheetOld
    13 {
    14     UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:@"111" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"犹豫" otherButtonTitles:@"确定", nil];
    15     
    16     [action showInView:self.view];
    17 }

      2、而从iOS8开始, UIAlertController = UIAlertView + UIAlertSheet

     1 #pragma mark -- new iOS8
     2 
     3 -(void)new
     4 {
     5     //1、创建中间的弹出:UIAlertView
     6     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleAlert];
     7     
     8     //创建底部的弹出:UIAlertSheet
     9     //UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleActionSheet];
    10     
    11     //2、添加按钮(不需要代理了)
    12     
    13     //这里需要防止循环引用(可以通过新建一个自己的控制器继承UIAlertController,在dealloc中打印信息,验证是否有最终释放)
    14     __weak typeof(alert) weakAlert = alert;
    15     [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    16         
    17         NSLog(@"%@",[weakAlert.textFields.firstObject text]);
    18         
    19     }]];
    20     [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    21     [alert addAction:[UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil]];
    22     
    23     //3、添加文本框(只能是添加到UIAlertControllerStyleAlert的样式,如果是preferredStyle:UIAlertControllerStyleActionSheet的话会崩溃的)
    24     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    25         
    26         textField.textColor =[UIColor redColor];
    27         textField.text = @"11111";
    28         
    29         //监听文字的改变(如果是用通知的话,移除的时机需要注意,点击确定或者取消的时候就需要去移除,比较麻烦)
    30         [textField addTarget:self action:@selector(textFieldsValueDidChange:) forControlEvents:UIControlEventEditingChanged];
    31     }];
    32     [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    33         
    34         textField.secureTextEntry = YES;
    35         textField.text = @"22222";
    36     }];
    37     
    38     //4、弹出控制器,要想是不是用modal
    39     [self presentViewController:alert animated:YES completion:nil];
    40 }
    41 
    42 -(void)textFieldsValueDidChange:(UITextField *)textField
    43 {
    44     NSLog(@"%@",textField.text);
    45 }

      3、在iPhone和iPad中同时适用的方式

     1 #pragma mark -- new iOS8 in iPad
     2 /**
     3  *  注意:
     4  UIAlertControllerStyleActionSheet
     5  1、不能有文本框
     6  2、在iPad中,必须使用popover的形式展示
     7  
     8  以后都是用present
     9  */
    10 -(void)newInpad
    11 {
    12     //1、创建底部的弹出:UIAlertSheet
    13     UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"弹出信息" preferredStyle:UIAlertControllerStyleActionSheet];
    14     
    15     // 1.1 这句代码可写可不写,因为默认就会以UIModalPresentationPopover的形式存在
    16     alert.modalPresentationStyle = UIModalPresentationPopover;
    17 
    18     // 1.2 设置popover指向按钮(这句代码iPhone中会被忽略,iPad中才会实现)
    19     alert.popoverPresentationController.barButtonItem = self.navigationItem.leftBarButtonItem;
    20     
    21     //2、添加按钮(不需要代理了)
    22     
    23     //这里需要防止循环引用(可以通过新建一个自己的控制器继承UIAlertController,在dealloc中打印信息,验证是否有最终释放)
    24     __weak typeof(alert) weakAlert = alert;
    25     [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    26         
    27         NSLog(@"%@",[weakAlert.textFields.firstObject text]);
    28         
    29     }]];
    30     [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    31     [alert addAction:[UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil]];
    32     
    33     //4、弹出控制器,要想是不是用modal
    34     [self presentViewController:alert animated:YES completion:nil];
    35 }

    注意:

     UIAlertControllerStyleActionSheet

       1、不能有文本框

       2、在iPad中,必须使用popover的形式展示

  • 相关阅读:
    【基于初学者】SSH+Maven实现Excel导出功能
    【基于初学者】IDEA中Git的使用
    【基于初学者】通过eclipse工具如何创建Spring Boot工程
    【基于初学者】基于ssm框架实现不同用户显示不同的菜单模块
    【基于初学者】Struts框架
    【基于初学者】Maven相关配置和创建
    树莓派2+无线网卡=钓鱼热点
    mockjs 项目实战踩坑
    上传文件 上传图片 源码跟思路
    css 浮动及清除浮动 详细讲解
  • 原文地址:https://www.cnblogs.com/daomul/p/4770457.html
Copyright © 2020-2023  润新知