IOS8以上的UIAlertView
UIAlertView是iOS开发中常用的弹窗控件,通常用于提示用户操作结果或进行确认操作。在iOS8以上的版本中,UIAlertController取代UIAlertView成为了首选的弹窗控件。本文将介绍iOS8以上版本中UIAlertController的使用方法。
1. 创建UIAlertController
使用UIAlertController的 步就是创建一个实例。UIAlertController可以创建两种样式的弹框,一种是UIAlertControllerStyleAlert,另一种是UIAlertControllerStyleActionSheet。其中UIAlertControllerStyleAlert用于显示一般的提示信息,而UIAlertControllerStyleActionSheet用于显示用户操作选择。
UIAlertController初始化需要提供样式类型和标题。初始化语句如下:
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"提示内容" preferredStyle:UIAlertControllerStyleAlert];
2. 添加UIAlertAction
UIAlertController弹窗中最重要的是UIAlertAction,它是用户与弹窗交互的主要方式。创建UIAlertAction需要提供三个参数:
参数名 | 说明
----|----
title | 按钮的标题
style | 按钮的风格,分为UIAlertActionStyleDefault,UIAlertActionStyleCancel和UIAlertActionStyleDestructive三种
handler | 按钮的回调方法,用户与UIAlertController进行交互时会执行该方法
UIAlertAction的风格分为三种,其中UIAlertActionStyleDefault表示默认风格,UIAlertActionStyleCancel表示取消风格,UIAlertActionStyleDestructive表示警告风格。UIAlertActionStyleCancel将默认在弹出视图中显示在底部,并具有特殊的标题和样式。
常见的 UIAlertAction 的实例化方法如下:
[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定按钮点击事件处理");
}]
3. 添加UIAlertAction到UIAlertController中
添加UIAlertAction到UIAlertController非常方便,只需要调用UIAlertController的-addAction:方法即可。例如,将上一个UIAlertAction添加到UIAlertController中的代码如下:
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定按钮点击事件处理");
}]];
添加完UIAlertAction之后,弹出UIAlertController的方法也非常简单。只需要调用当前ViewController的presentViewController:animated:completion:方法即可。示例如下:
[self presentViewController:alertController animated:YES completion:nil];
在弹出的UIAlertController中,可以根据需要添加任意数量的UIAlertAction。所有的UIAlertAction会按照在代码中添加的顺序显示在弹框中。如果加入了多个UIAlertAction,UIAlertController会自动将它们按照iOS系统的标准进行布局,在iPhone上,通常会显示2到3个按钮,而在iPad上,UIAlertController经常使用popover视图显示。
4. 处理UIAlertAction的回调
当用户点击了某个UIAlertAction之后,就需要调用该UIAlertAction的handler方法进行后续操作。例如,在点击“确定”按钮之后,需要执行某些逻辑,可以在handler方法中添加相关的代码。示例如下:
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"确定按钮点击事件处理");
// 添加具体的逻辑代码
[self doSomething];
}]];
当用户点击“确定”按钮时,doSomething方法就会被执行。需要注意的是,在UIAlertController中处理UIAlertAction的回调方法时,一定要小心,保证不会出现内存泄露和其他问题。
5. 案例:显示一个简单的UIAlertController
最后,让我们来演示一下如何创建一个简单的UIAlertView。在该UIAlertController中,用户可以根据是否删除某个选项,做出不同的选择。
// 创建UIAlertController
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"您是否要删除这个选项?" preferredStyle:UIAlertControllerStyleAlert];
// 添加UIAlertAction
[alertController addAction:[UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
NSLog(@"删除选项");
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
NSLog(@"取消选择");
}]];
// 弹出UIAlertController
[self presentViewController:alertController animated:YES completion:nil];
通过上述代码,就可以创建一个简单的带有两个按钮的UIAlertController,并在用户点击某个按钮时执行相应的处理逻辑。将UIAlertController和UIAlertAction组合起来,可以为iOS应用程序提供一个简洁、灵活、易用的弹出框架,大大提高了用户交互的体验。
