UIAlertView
Objective-C版本:
1
2
|
UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@ "标题" message:@ "这个是UIAlertView的默认样式" delegate:self cancelButtonTitle:@ "取消" otherButtonTitles:@ "好的" , nil];
[alertview show];
|
UIAlertView的默认样式
swift版本和Objective-C版本不同,在swift中,alertView的初始化只允许创建拥有一个取消按钮的对话框视图。或许您可以看到带有otherButtonTitles的init方法,但是很遗憾,这个方法是没有办法通过编译的。
1
2
|
var alertView = UIAlertView(title: "标题" , message: "这个是UIAlertView的默认样式" , delegate: self, cancelButtonTitle: "取消" )
alertView.show()
|
swift版本的UIAlertView
要能够创建和上面Objective-C版本相同的对话框视图,我们可以采取曲线救国的方法,虽然麻烦了些,但是我们为了目的可以不择手段的,是吧?
1
2
3
4
5
6
7
|
var alertView = UIAlertView()
alertView.delegate = self
alertView.title = "标题"
alertView.message = "这个是UIAlertView的默认样式"
alertView.addButtonWithTitle( "取消" )
alertView.addButtonWithTitle( "好的" )
alertView.show()
|
您也可以通过更改UIAlertView的alertViewStyle属性来实现输入文字、密码甚至登录框的效果。
UIAlertView文本对话框
UIAlertView密码对话框
UIAlertView登录对话框
UIAlertViewDelegate协议拥有响应对话框视图的按钮动作的回调方法。还有当文本框内容改变时,调用alertViewShouldEnableOtherButton:方法可以让按钮动态地可用或者不可用。
要说明一点,苹果官方现在并不提倡在iOS
8中使用UIAlertView,取而代之的是UIAlertController。下面我们就来介绍UIAlertController的使用方法。
UIAlertController
在iOS
8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action
sheet),就取决于在创建控制器时,您是如何设置首选样式的。
一个简单的对话框例子
您可以比较一下两种不同的创建对话框的代码,创建基础UIAlertController的代码和创建UIAlertView的代码非常相似:
Objective-C版本:
1
|
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@ "标题" message:@ "这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];
|
swift版本:
1
|
var alertController = UIAlertController(title: "标题" , message: "这个是UIAlertController的默认样式" , preferredStyle: UIAlertControllerStyle.Alert)
|
同创建UIAlertView相比,我们无需指定代理,也无需在初始化过程中指定按钮。不过要特别注意第三个参数,要确定您选择的是对话框样式还是上拉菜单样式。
通过创建UIAlertAction的实例,您可以将动作按钮添加到控制器上。UIAlertAction由标题字符串、样式以及当用户选中该动作时运行的代码块组成。通过UIAlertActionStyle,您可以选择如下三种动作样式:常规(default)、取消(cancel)以及警示(destruective)。为了实现原来我们在创建UIAlertView时创建的按钮效果,我们只需创建这两个动作按钮并将它们添加到控制器上即可。
Objective-C版本:
1
2
3
4
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@ "取消" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@ "好的" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
|
swift版本:
1
2
3
4
|
var cancelAction = UIAlertAction(title: "取消" , style: UIAlertActionStyle.Cancel, handler: nil)
var okAction = UIAlertAction(title: "好的" , style: UIAlertActionStyle.Default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(okAction)
|
最后,我们只需显示这个对话框视图控制器即可:
Objective-C版本:
1
|
[self presentViewController:alertController animated:YES completion:nil];
|
swift版本:
1
|
self.presentViewController(alertController, animated: true , completion: nil)
|
UIAlertController默认样式
“警示”样式
什么是“警示”样式呢?我们先不着急回答这个问题,先来看一下下面关于“警示”样式的简单示例。在这个示例中,我们将前面的示例中的“好的”按钮替换为了“重置”按钮。
Objective-C版本:
1
2
|
UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@ "重置" style:UIAlertActionStyleDestructive handler:nil];
[alertController addAction:resetAction];
|
swift版本:
1
2
|
var resetAction = UIAlertAction(title: "重置" , style: UIAlertActionStyle.Destructive, handler: nil)
alertController.addAction(resetAction)
|
“警示”样式
可以看出,我们新增的那个“重置”按钮变成了红色。根据苹果官方的定义,“警示”样式的按钮是用在可能会改变或删除数据的操作上。因此用了红色的醒目标识来警示用户。
|