UIAlertcontroller,UIAlertView

随着苹果上次iOS 5的发布,对话框视图样式出如今了咱们面前,直到如今它都没有发生过很大的变化。下面的代码片断展现了如何初始化和显示一个带有“取消”和“好的”按钮的对话框视图。swift

Objective-C版本:安全

UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"标题" message:@"这个是UIAlertView的默认样式" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"好的", nil];
[alertview show];

swift版本和Objective-C版本不一样,在swift中,alertView的初始化只容许建立拥有一个取消按钮的对话框视图。或许您能够看到带有otherButtonTitles的init方法,可是很遗憾,这个方法是没有办法经过编译的。app

var alertView = UIAlertView(title: "标题", message: "这个是UIAlertView的默认样式", delegate: self, cancelButtonTitle: "取消")
alertView.show()

要可以建立和上面Objective-C版本相同的对话框视图,咱们能够采起曲线救国的方法,虽然麻烦了些,可是咱们为了目的能够不择手段的,是吧?模块化

var alertView = UIAlertView()
alertView.delegate = selfalertView.title = "标题"alertView.message = "这个是UIAlertView的默认样式"alertView.addButtonWithTitle("取消")
alertView.addButtonWithTitle("好的")
alertView.show()

您也能够经过更改UIAlertViewalertViewStyle属性来实现输入文字、密码甚至登陆框的效果。spa

UIAlertViewDelegate协议拥有响应对话框视图的按钮动做的回调方法。还有当文本框内容改变时,调用alertViewShouldEnableOtherButton:方法可让按钮动态地可用或者不可用。代理

要说明一点,苹果官方如今并不提倡在iOS 8中使用UIAlertView,取而代之的是UIAlertController。下面咱们就来介绍UIAlertController的使用方法。code

UIAlertController

在iOS 8中,UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和做用。是使用对话框(alert)仍是使用上拉菜单(action sheet),就取决于在建立控制器时,您是如何设置首选样式的。orm

一个简单的对话框例子

您能够比较一下两种不一样的建立对话框的代码,建立基础UIAlertController的代码和建立UIAlertView的代码很是类似:server

Objective-C版本:对象

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"这个是UIAlertController的默认样式" preferredStyle:UIAlertControllerStyleAlert];

swift版本:

var alertController = UIAlertController(title: "标题", message: "这个是UIAlertController的默认样式", preferredStyle: UIAlertControllerStyle.Alert)

同建立UIAlertView相比,咱们无需指定代理,也无需在初始化过程当中指定按钮。不过要特别注意第三个参数,要肯定您选择的是对话框样式仍是上拉菜单样式。

经过建立UIAlertAction的实例,您能够将动做按钮添加到控制器上。UIAlertAction由标题字符串、样式以及当用户选中该动做时运行的代码块组成。经过UIAlertActionStyle,您能够选择以下三种动做样式:常规(default)、取消(cancel)以及警示(destruective)。为了实现原来咱们在建立UIAlertView时建立的按钮效果,咱们只需建立这两个动做按钮并将它们添加到控制器上便可。

Objective-C版本:

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];

[alertController addAction:cancelAction];
[alertController addAction:okAction];

swift版本:

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版本:

[self presentViewController:alertController animated:YES completion:nil];

swift版本:

self.presentViewController(alertController, animated: true, completion: nil)

按钮显示的次序取决于它们添加到对话框控制器上的次序。通常来讲,根据苹果官方制定的《iOS 用户界面指南》,在拥有两个按钮的对话框中,您应当将取消按钮放在左边。要注意,取消按钮是惟一的,若是您添加了第二个取消按钮,那么你就会获得以下的一个运行时异常:

* Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘UIAlertController can only have one action with a style of UIAlertActionStyleCancel’

异常信息简洁明了,咱们在此就不赘述了。

“警示”样式

什么是“警示”样式呢?咱们先不着急回答这个问题,先来看一下下面关于“警示”样式的简单示例。在这个示例中,咱们将前面的示例中的“好的”按钮替换为了“重置”按钮。

Objective-C版本:

UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];

[alertController addAction:resetAction];

swift版本:

var resetAction = UIAlertAction(title: "重置", style: UIAlertActionStyle.Destructive, handler: nil)

alertController.addAction(resetAction)

“警示”样式

“警示”样式

能够看出,咱们新增的那个“重置”按钮变成了红色。根据苹果官方的定义,“警示”样式的按钮是用在可能会改变或删除数据的操做上。所以用了红色的醒目标识来警示用户。

文本对话框

UIAlertController极大的灵活性意味着您没必要拘泥于内置样式。之前咱们只能在默认视图、文本框视图、密码框视图、登陆和密码输入框视图中选择,如今咱们能够向对话框中添加任意数目的UITextField对象,而且可使用全部的UITextField特性。当您向对话框控制器中添加文本框时,您须要指定一个用来配置文本框的代码块。

举个栗子吧,要从新创建原来的登陆和密码样式对话框,咱们能够向其中添加两个文本框,而后用合适的占位符来配置它们,最后将密码输入框设置使用安全文本输入。

Objective-C版本:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登陆和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
    textField.placeholder = @"登陆";
}];

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    textField.placeholder = @"密码";
    textField.secureTextEntry = YES;
}];

swift版本:

alertController.addTextFieldWithConfigurationHandler { 
(textField: UITextField!) -> Void in
    textField.placeholder = "登陆"}

alertController.addTextFieldWithConfigurationHandler { 
(textField: UITextField!) -> Void in
    textField.placeholder = "密码"
    textField.secureTextEntry = true}

在“好的”按钮按下时,咱们让程序读取文本框中的值。

Objective-C版本:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {    UITextField *login = alertController.textFields.firstObject;    UITextField *password = alertController.textFields.lastObject;
    ...
}];

swift版本:

var okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) {
(action: UIAlertAction!) -> Void in
    var login = alertController.textFields?.first as UITextField
    var password = alertController.textFields?.last as UITextField}

若是咱们想要实现UIAlertView中的委托方法alertViewShouldEnableOtherButton:方法的话可能会有一些复杂。假定咱们要让“登陆”文本框中至少有3个字符才能激活“好的”按钮。很遗憾的是,在UIAlertController中并无相应的委托方法,所以咱们须要向“登陆”文本框中添加一个Observer。Observer模式定义对象间的一对多的依赖关系,当一个对象的状态发生改变时, 全部依赖于它的对象都获得通知并被自动更新。咱们能够在构造代码块中添加以下的代码片断来实现。

Objective-C版本:

[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
    ...    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];}];

swift版本:

alertController.addTextFieldWithConfigurationHandler {
(textField: UITextField!) -> Void in
    ...    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("alertTextFieldDidChange:"), name: UITextFieldTextDidChangeNotification, object: textField)
}

当视图控制器释放的时候咱们须要移除这个Observer,咱们经过在每一个按钮动做的handler代码块(还有其余任何可能释放视图控制器的地方)中添加合适的代码来实现它。好比说在okAction这个按钮动做中:

Objective-C版本:

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    ...
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}];

swift版本:

var okAction = UIAlertAction(title: "好的", style: UIAlertActionStyle.Default) {
(action: UIAlertAction!) -> Void in
    ...    NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextFieldTextDidChangeNotification, object: nil)
}

在显示对话框以前,咱们要冻结“好的”按钮

Objective-C版本:

okAction.enabled = NO;

swift版本:

okAction.enabled = false

接下来,在通知观察者(notification observer)中,咱们须要在激活按钮状态前检查“登陆”文本框的内容。

Objective-C版本:

- (void)alertTextFieldDidChange:(NSNotification *)notification{    UIAlertController *alertController = (UIAlertController *)self.presentedViewController;    if (alertController) {        UITextField *login = alertController.textFields.firstObject;        UIAlertAction *okAction = alertController.actions.lastObject;
        okAction.enabled = login.text.length > 2;
    }
}

swift版本:

func alertTextFieldDidChange(notification: NSNotification){    var alertController = self.presentedViewController as UIAlertController?    if (alertController != nil) {        var login = alertController!.textFields?.first as UITextField
        var okAction = alertController!.actions.last as UIAlertAction
        okAction.enabled = countElements(login.text) > 2
    }
}


UIAlertController的登陆和密码对话框示例

UIAlertController的登陆和密码对话框示例

好了,如今对话框的“好的”按钮被冻结了,除非在“登陆”文本框中输入3个以上的字符:

相关文章
相关标签/搜索