一、概述
iOS开发中,经常需要在app中使用弹窗提示用户信息或者提供选择操作,UIAlertController提供了一种简单易用的方式来实现这些功能。UIAlertController可以在视图控制器中以模态展示的形式展示警告框、操作表、文本框等。本文将详细介绍UIAlertController的使用方法,以及如何自定义警告框的样式和动作。
二、UIAlertController的基本用法
在iOS中,UIAlertController有两个主要的类型:UIAlertControllerStyleAlert
(警告框)和UIAlertControllerStyleActionSheet
(操作表)。
(一)UIAlertControllerStyleAlert的使用
UIAlertControllerStyleAlert
警告框的创建方法如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
其中,title
参数可选,用于设置警告框的标题,message
参数也可选,用于设置警告框的提示信息。
下面是创建UIAlertControllerStyleAlert
的简单示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//删除操作
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
如上代码所示,首先创建了一个UIAlertController
的实例,并设置了标题和提示内容,然后创建两个UIAlertAction
的实例,cancelAction
表示“取消”操作,deleteAction
表示“删除”操作。addAction
方法可以为UIAlertController
添加操作,最后使用presentViewController:animated:completion:
方法来展示UIAlertController
。
运行上述代码,会弹出一个警告框,如下图所示:
(二)UIAlertControllerStyleActionSheet的使用
UIAlertControllerStyleActionSheet
操作表的创建方式如下:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
其中,title
和message
都设置为nil,因为操作表没有标题和提示信息。下面是创建UIAlertControllerStyleActionSheet
的示例:
UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//拍照操作
}];
UIAlertAction *photoLibraryAction = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//从相册选择操作
}];
[actionSheetController addAction:cancelAction];
[actionSheetController addAction:cameraAction];
[actionSheetController addAction:photoLibraryAction];
[self presentViewController:actionSheetController animated:YES completion:nil];
如上代码所示,首先创建了一个UIAlertController
的实例,然后添加了三个UIAlertAction
的实例,cancelAction
表示“取消”操作,cameraAction
表示“拍照”操作,photoLibraryAction
表示“从相册选择”操作,最后使用presentViewController:animated:completion:
方法来展示UIAlertController
。
运行上述代码,会弹出一个操作表,如下图所示:
三、自定义UIAlertController的外观和操作
(一)自定义UIAlertController的外观
UIAlertController
提供了多种方式来修改警告框和操作表的外观,例如修改标题颜色和字体、修改按钮颜色和字体等。下面是一个修改UIAlertControllerStyleAlert
外观的简单示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除?" preferredStyle:UIAlertControllerStyleAlert];
//修改标题颜色和字体
NSMutableAttributedString *titleAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"提示" attributes:@{NSForegroundColorAttributeName:[UIColor redColor],NSFontAttributeName:[UIFont systemFontOfSize:20]}];
[alertController setValue:titleAttributedStr forKey:@"attributedTitle"];
//修改提示内容颜色和字体
NSMutableAttributedString *messageAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"是否确认删除?" attributes:@{NSForegroundColorAttributeName:[UIColor blueColor],NSFontAttributeName:[UIFont systemFontOfSize:18]}];
[alertController setValue:messageAttributedStr forKey:@"attributedMessage"];
//修改“取消”按钮的颜色和字体
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[alertController addAction:cancelAction];
//修改“删除”按钮的颜色和字体
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//删除操作
}];
[deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController addAction:deleteAction];
[self presentViewController:alertController animated:YES completion:nil];
如上代码所示,使用setValue:forKey:
方法来修改UIAlertController
的属性值,其中key
值是属性名,value
值是修改后的属性值。运行上述代码,会弹出一个外观自定义的UIAlertController
,如下图所示:
(二)自定义UIAlertController的操作
除了可以自定义UIAlertController
的外观之外,还可以自定义UIAlertController
的操作。UIAlertController
提供了多种方式来处理按钮点击事件,例如执行一些代码、跳转页面等。下面是一个自定义UIAlertController
操作的简单示例:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确认删除?" preferredStyle:UIAlertControllerStyleAlert];
//修改“取消”按钮的颜色和字体
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[cancelAction setValue:[UIColor greenColor] forKey:@"titleTextColor"];
[alertController addAction:cancelAction];
//修改“删除”按钮的颜色和字体
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//删除操作
NSLog(@"删除操作");
}];
[deleteAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
[alertController addAction:deleteAction];
//添加文本框
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = @"请输入密码";
}];
[self presentViewController:alertController animated:YES completion:nil];
如上代码所示,使用addAction
方法来添加UIAlertAction
的实例,使用addTextFieldWithConfigurationHandler
方法来添加文本框。在删除操作的handler
中,可以执行想要的代码,例如执行删除操作,或是重新加载页面。运行上述代码,会弹出一个带有文本框的警告框,并且在删除操作时会打印一条日志,如下图所示:
四、总结
UIAlertController
是一种简单易用的方式来实现弹窗提示和操作选择功能。它提供了两种主要的类型:UIAlertControllerStyleAlert
(警告框)和UIAlertControllerStyleActionSheet
(操作表),并且可以通过修改属性来自定义警告框和操作表的外观样式,也可以通过添加文本框或是自定义操作来扩展UIAlertController
的功能。