[分享]iOS开发-UIAlertControlle和UIAlertAction的使用

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


UIAlertControllerios

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

一个简单的对话框例子app

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

Objective-C版本:spa

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

swift版本:代理

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

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

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

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)

1416967052393203.png

UIAlertController默认样式

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

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

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


demo代码:

-(void)deleted
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"肯定要删除当前水票吗?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    //handler为手势方法添加处,这里就代替了以前UIAlertView中的代理协议方法,十分方便
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"肯定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self okDeleted];
    }];
    [alert addAction:cancelAction];
    [alert addAction:okAction];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)okDeleted
{
    DLog(@"点到删除啦 啊啦啦啦啦");
}

参考来源:


http://www.cocoachina.com/ios...

相关文章
相关标签/搜索