UIAlertController 警告视图控制器

前言

  • Objective-C

    1
    NS_CLASS_AVAILABLE_IOS(8_0) @interface UIAlertController : UIViewController
  • Swift

    1
    @available(iOS 8.0, *) public class UIAlertController : UIViewController

1、alertController 的创建

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    // 1. 创建时不添加按钮

    // 实例化 alertController 对象
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告"
    message:@"真的要关闭 !"
    preferredStyle:UIAlertControllerStyleAlert];

    // 显示,模态视图显示
    [self presentViewController:alertController animated:YES completion:nil];

    // 2. 创建时添加按钮等信息

    // 实例化 UIAlertController 对象
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告"
    message:@"真的要关闭 !"
    preferredStyle:UIAlertControllerStyleAlert];

    // 创建按钮
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
    style:UIAlertActionStyleCancel
    handler:nil];

    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定"
    style:UIAlertActionStyleDefault
    handler:nil];
    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"考虑一下"
    style:UIAlertActionStyleDestructive
    handler:nil];
    // 向 alertController 上添加按钮
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [alertController addAction:noAction];

    // 显示 alertController 视图
    [self presentViewController:alertController animated:YES completion:nil];
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    // 1. 创建时不添加按钮

    // 实例化 alertController 对象
    let alertController: UIAlertController = UIAlertController(title: "警告",
    message: "真的要关闭 !",
    preferredStyle: .Alert)

    // 显示,模态视图显示
    self.presentViewController(alertController, animated: true, completion: nil)

    // 2. 创建时添加按钮等信息

    // 实例化 alertController 对象
    let alertController: UIAlertController = UIAlertController(title: "警告",
    message: "真的要关闭 !",
    preferredStyle: .Alert)

    // 创建按钮
    let cancelAction: UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
    let okAction: UIAlertAction = UIAlertAction(title: "确定", style: .Default, handler: nil)
    let noAction: UIAlertAction = UIAlertAction(title: "考虑一下", style: .Destructive, handler: nil)

    // 向 alertController 上添加按钮
    alertController.addAction(cancelAction)
    alertController.addAction(okAction)
    alertController.addAction(noAction)

    // 显示 alertController 视图
    self.presentViewController(alertController, animated: true, completion: nil)

2、alertController 的设置

  • Objective-C

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    // 设置警告框类型
    /*
    UIAlertControllerStyleActionSheet = 0, 上拉菜单,操作表,底部弹出
    UIAlertControllerStyleAlert 对话框,警告,中间弹出
    */
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"警告"
    message:@"真的要关闭 ?"
    preferredStyle:UIAlertControllerStyleAlert];

    // 设置按钮类型
    /*
    UIAlertActionStyleDefault = 0, 默认蓝色按钮,可以有多个
    UIAlertActionStyleCancel, 取消按钮,显示在左侧或最下边,有且只能有一个
    UIAlertActionStyleDestructive 红色警示按钮,可以有多个,
    《iOS 用户界面指南》要求所有的 “警示” 样式按钮都必须排名第一
    */
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
    style:UIAlertActionStyleCancel
    handler:nil];

    // 设置按钮点击响应事件
    UIAlertAction *closeAction = [UIAlertAction actionWithTitle:@"关闭"
    style:UIAlertActionStyleDestructive
    handler:^(UIAlertAction * _Nonnull action) {

    /*
    点击了按钮时响应的事件
    */
    }];

    // 添加输入框
    /*
    只能添加到 UIAlertControllerStyleAlert 上,可以添加多个
    */
    [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {

    /*
    设置添加的 textField 属性
    */
    }];

    // 添加点击按钮
    /*
    添加警告框上的按钮,可以添加多个
    取消按钮在左侧或最下边,其它按钮按照添加的顺序排列
    */
    [alertController addAction:cancelAction];

    // 设置首选按钮
    /*
    必须在添加按钮(addAction)完成后设置,iOS 9 新添加
    首选的按钮会有加粗效果,只能用在 UIAlertControllerStyleAlert 中
    */
    alertController.preferredAction = okAction;

    // 设置按钮激活状态
    /*
    YES 按钮激活,可点击。NO 按钮禁用,不可点击
    */
    okAction.enabled = YES;

    // 显示警告框视图
    [self presentViewController:alertController animated:YES completion:nil];

    // 设置警告框标题
    alertController.title = @"登录";

    // 设置警告框提示信息
    alertController.message = @"请输入用户名和密码登录 !";

    // 获取警告框标题
    NSString *alertTitle = alertController.title;

    // 获取警告框提示信息
    NSString *alertMessage = alertController.message;

    // 获取警告框类型,readonly
    UIAlertControllerStyle alertStyle = alertController.preferredStyle;

    // 获取所有输入框,readonly
    NSArray<UITextField *> *textFieldArray = alertController.textFields;

    // 获取所有按钮,readonly
    NSArray<UIAlertAction *> *actionsArray = alertController.actions;

    // 获取按钮标题,readonly
    NSString *actionTitle = okAction.title;

    // 获取按钮类型,readonly
    UIAlertActionStyle actionStyle = okAction.style;

    // 获取首选按钮
    UIAlertAction *preferredAction = alertController.preferredAction;

    // 获取按钮激活状态
    BOOL actionEnabled = okAction.enabled;
  • Swift

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    // 设置警告框类型
    /*
    case ActionSheet 上拉菜单,操作表,底部弹出
    case Alert 对话框,警告,中间弹出
    */
    let alertController:UIAlertController = UIAlertController(title: "警告",
    message: "真的要关闭 !",
    preferredStyle: .Alert)

    // 设置按钮类型
    /*
    case Default 默认蓝色按钮,可以有多个
    case Cancel 取消按钮,显示在左侧或最下边,有且只能有一个
    case Destructive 红色警示按钮,可以有多个,
    《iOS 用户界面指南》要求所有的 “警示” 样式按钮都必须排名第一
    */
    let cancelAction:UIAlertAction = UIAlertAction(title: "取消", style: .Cancel, handler: nil)

    // 设置按钮点击响应事件
    let closeAction:UIAlertAction = UIAlertAction(title: "关闭",
    style: .Destructive) { (action:UIAlertAction) in

    /*
    点击了按钮时响应的事件
    */
    }

    // 添加输入框
    /*
    只能添加到 UIAlertControllerStyleAlert 上,可以添加多个
    */
    alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) in

    /*
    设置添加的 textField 属性
    */
    }

    // 添加点击按钮
    /*
    添加警告框上的按钮,可以添加多个
    取消按钮在左侧或最下边,其它按钮按照添加的顺序排列
    */
    alertController.addAction(cancelAction)

    // 设置首选按钮
    /*
    必须在添加按钮(addAction)完成后设置,iOS 9 新添加
    首选的按钮会有加粗效果,只能用在 UIAlertControllerStyleAlert 中
    */
    alertController.preferredAction = okAction

    // 设置按钮激活状态
    /*
    YES 按钮激活,可点击。NO 按钮禁用,不可点击
    */
    okAction.enabled = true

    // 显示警告框视图
    self.presentViewController(alertController, animated: true, completion: nil)

    // 设置警告框标题
    alertController.title = "登录"

    // 设置警告框提示信息
    alertController.message = "请输入用户名和密码登录 !"

    // 获取警告框标题
    let alertTitle:String? = alertController.title

    // 获取警告框提示信息
    let alertMessage:String? = alertController.message

    // 获取警告框类型,readonly
    let alertStyle:UIAlertControllerStyle = alertController.preferredStyle

    // 获取所有输入框,readonly
    let textFieldArray:[UITextField]? = alertController.textFields

    // 获取所有按钮,readonly
    let actionsArray:[UIAlertAction] = alertController.actions

    // 获取按钮标题,readonly
    let actionTitle:String? = okAction.title

    // 获取按钮类型,readonly
    let actionStyle:UIAlertActionStyle = okAction.style

    // 获取首选按钮
    let preferredAction:UIAlertAction? = alertController.preferredAction

    // 获取按钮激活状态
    let actionEnabled:Bool = okAction.enabled
文章目录
  1. 1. 前言
  2. 2. 1、alertController 的创建
  3. 3. 2、alertController 的设置
隐藏目录