iOS-对话框的应用

在应用的开发过程中我们会经常使用到提示对话框,这里我们对iOS自带的提示对话框机制进行一个探讨。

  由于在iOS9.0以后官方推出了UIAlertController,这个类使用起来比以往的UIAlertViewUIActionSheet都更简单更强大,所以我们会先介绍用UIAlertController来实现的两种提示对话框。然后再回顾UIAlertViewUIActionSheet的使用方法。

首先我们先要了解官方自带的提示对话框主要有两种形式,一种是居中的对话框,一种是从底部弹出的对话框。在UIAlertController中定义了两个变量:

UIAlertControllerStyleActionSheet
UIAlertControllerStyleAlert

UIAlertController

首先我们先来看居中的提示框的实现。

//创建一个UIAlertController对象
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示框" message:@"AlertController实现的提示框" preferredStyle:UIAlertControllerStyleAlert];

//创建一个确定按钮及其点击触发事件
UIAlertAction *submit = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //这里是点击确定按钮的操作
    NSLog(@"点击确定按钮.");
}];

//创建一个取消按钮
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

//将创建的两个按钮添加到UIAlertController对象中
[alert addAction:submit];
[alert addAction:cancle];

//显示弹窗
[self presentViewController:alert animated:YES completion:nil];

其实从底部弹出的提示框和上面提到的居中的提示框的代码几乎是一模一样的。主要就是在alertControllerWithTitle: message: preferredStyle:消息中第三个参数传参值为UIAlertControllerStyleActionSheet

//创建一个UIAlertController对象
UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示框" message:@"AlertController实现的提示框2" preferredStyle:UIAlertControllerStyleActionSheet];

//创建一个确定按钮及其点击触发事件
UIAlertAction *sheet1 = [UIAlertAction actionWithTitle:@"Sheet 1" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //这里是点击确定按钮的操作
    NSLog(@"Sheet 1.");
}];

UIAlertAction *sheet2 = [UIAlertAction actionWithTitle:@"Sheet 2" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //这里是点击确定按钮的操作
    NSLog(@"Sheet 2.");
}];

UIAlertAction *sheet3 = [UIAlertAction actionWithTitle:@"Sheet 3" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    //这里是点击确定按钮的操作
    NSLog(@"Sheet 3.");
}];

//创建一个取消按钮
UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

//将创建的两个按钮添加到UIAlertController对象中
[alert2 addAction:sheet1];
[alert2 addAction:sheet2];
[alert2 addAction:sheet3];
[alert2 addAction:cancle];

//显示弹窗
[self presentViewController:alert2 animated:YES completion:nil];

UIAlertView

尽管在iOS9.0以后这个类就不推荐使用了,但是我们作为学习还是可以了解一下的。UIAlertView对象的初始化很简单,还需要实现对应的UIAlertViewDelegate协议,然后在该协议中可以响应一些按钮点击事件之类的。

//创建UIAlertView对象
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示框" message:@"AlertView" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//显示弹窗
[alert show];

然后接下来是实现协议UIAlertViewDelegate的部分方法。

#pragma mark UIAlertViewDelegate委托实现
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            NSLog(@"点击取消按钮.");
            break;

        case 1:
            NSLog(@"点击确定按钮.");
            break;

        default:
            break;
    }
}

UIActionSheet

这就是从底部弹出的提示对话框的旧实现方法。

    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"提示框" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"Sheet1" otherButtonTitles:@"Sheet2", nil];
//显示弹窗
[sheet showInView:self.view];

然后是UIAlertSheetDelegate协议的实现。

#pragma mark UIAlertSheetDelegate委托实现
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 2:
            NSLog(@"取消");
            break;

        case 0:
            NSLog(@"Sheet1");
            break;

        case 1:
            NSLog(@"Sheet2");
            break;

        default:
            break;
    }
}

总结

通过对比我们发现,新推出的UIAlertController的确比原来的实现方法要更简单,更好用。并且结合了块的概念,提供更好的可读性和可扩展性。