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;// 获取警告框类型,readonlyUIAlertControllerStyle alertStyle = alertController.preferredStyle;// 获取所有输入框,readonlyNSArray *textFieldArray = alertController.textFields;// 获取所有按钮,readonlyNSArray *actionsArray = alertController.actions;// 获取按钮标题,readonlyNSString *actionTitle = okAction.title;// 获取按钮类型,readonlyUIAlertActionStyle actionStyle = okAction.style;// 获取首选按钮UIAlertAction *preferredAction = alertController.preferredAction;// 获取按钮激活状态BOOL actionEnabled = okAction.enabled;