UIKit框架-07.UIAlertController使用

来源:互联网 发布:手机视频保密软件 编辑:程序博客网 时间:2024/05/17 21:55

1.UIAlertController概述

  • UIAlertController是IOS 8.0之后出现的新特性,它同时结合了UIAlertView和UIActionSheet的功能
  • IOS 8.0之前我们还是需要使用UIAlertView和UIActionSheet,完成弹框功能

2.使用UIAlertController实现UIAlertView效果

  • 实现UIAlerView
-(void)touchesBegan:(nonnull NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event{    // 1.创建UIAlertControllerStyleAlert样式的UIAlertController    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert];    // 2.创建按钮    /*     UIAlertActionStyleDefault = 0, //默认样式     UIAlertActionStyleCancel,      // 字体加粗     UIAlertActionStyleDestructive  // 强调,字体标红     */    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action1被点击了");    }];    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action2被点击了");    }];    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action3被点击了");    }];    // 2.1 添加按钮    [alertVc addAction:action1];    [alertVc addAction:action2];    [alertVc addAction:action3];    // 3.添加文本框    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框1");    }];    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框2");        textField.secureTextEntry = YES;//设置密文    }];    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框3");    }];    //4.添加显示控制器方法    [self presentViewController:alertVc animated:YES completion:nil];}
  • 效果图
    这里写图片描述

  • 注意:我们使用UIAlertView最多只能创建2个文本输入框,而实用UIAlertController可以创建多个文本输入框


3.实现UIActionSheet效果

  • 实现UIActionSheet
-(void)setAlertview{    // 1.创建UIAlertControllerStyleAlert样式的UIAlertController    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"标题" message:@"正文" preferredStyle:UIAlertControllerStyleAlert];    // 2.创建按钮    /*     UIAlertActionStyleDefault = 0, //默认样式     UIAlertActionStyleCancel,      // 字体加粗     UIAlertActionStyleDestructive  // 强调,字体标红     */    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"按钮1" style:UIAlertActionStyleCancel handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action1被点击了");    }];    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"按钮2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action2被点击了");    }];    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"按钮3" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * __nonnull action) {// 点击事件        NSLog(@"action3被点击了");    }];    // 2.1 添加按钮    [alertVc addAction:action1];    [alertVc addAction:action2];    [alertVc addAction:action3];    // 3.添加文本框    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框1");    }];    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框2");        textField.secureTextEntry = YES;//设置密文    }];    [alertVc addTextFieldWithConfigurationHandler:^(UITextField * __nonnull textField) {        NSLog(@"文本框3");    }];    //4.添加显示控制器方法    [self presentViewController:alertVc animated:YES completion:nil];}
  • 效果图
    这里写图片描述

  • 注意:

    • 如果UIAlertController的样式是ActionSheet, 就不能添加输入框

       reason: 'Text fields can only be added to an alert controller of style UIAlertControllerStyleAlert'
    • UIAlertController继承UIController类,而UIAlertView和UIActionSheet继承UIView,所以要想在视图上显示UIAlertController,需要调用如下方法

    • [self presentViewController:alertVc animated:YES completion:nil];
0 0
原创粉丝点击