UIAlertController

1、alertController 的创建

// 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];

2、alertController 的设置

// 设置警告框类型
/*
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;
原文地址:https://www.cnblogs.com/CH520/p/9413502.html