iOS UIAlertController

在Xcode的iOS9.0 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代。

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

1、对话框

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

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

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

2、将动作按钮添加到控制器

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

   UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"message:@"这个是UIAlertController的默认样式"preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];

3、“警示”样式

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题"message:@"这个是UIAlertController的默认样式"preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];

    //添加顺序和显示顺序相同
    [alertController addAction:cancelAction];
    [alertController addAction:resetAction];
    [self presentViewController:alertController animated:YES completion:nil];

4、文本对话框

设置输入框,并且设置通知,检测输入字符长度少于3时,不能点击“好的”。

值得注意的是,不像使用UIAlertView的时候可以用UIAlertViewDelegate检测文本框输入,UIAlertController没有对应的代理,只能自己实现监听。

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        textField.placeholder = @"登录";
    }];

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];

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

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];

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

- (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;
    }
}

5、上拉菜单

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"保存或删除数据" message:@"删除数据将不可恢复" preferredStyle: UIAlertControllerStyleActionSheet];

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
        NSLog(@"点击了取消");
    }];

    UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
        NSLog(@"点击了删除");
    }];
    UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"保存" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
        NSLog(@"点击了保存");
    }];

    [alertController addAction:cancelAction];
    [alertController addAction:deleteAction];
    [alertController addAction:archiveAction];

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

时间: 2024-08-10 23:22:00

iOS UIAlertController的相关文章

IOS UIAlertController 使用方法

在很多种语言中,alert都表示弹窗操作,弹窗功能非常有用,不仅可以用于正式的app功能中,也可以在调试中使用.在OC中,UIAlertController类用来控制弹窗操作.在IOS 8.0之前, 其使用UIAlertView类来控制弹窗,如果我们想弹出一个带有输入框的窗口,可以使用如下的代码: // Prior to iOS 8.0 UIAlertView *alert = [[UIAlertView init] initWithTitle:@"Alert Title" messa

IOS UIAlertController 弹框 (ios 9.0 后代替了UIAlertView弹框 和 UIActionSheet下弹框)

在IOS 9.0 后 苹果官方宣布不再或不推荐使用UIAlertView 和 UIActionSheet 由UIAlertController进行代替两者 用控制器将两者合二为一 很简单 方便 下面就是关于UIAlertView的常用方法 #import "RootViewController.h" @interface RootViewController () @end @implementation RootViewController - (void)viewDidLoad {

iOS - UIAlertController三种显示提示框代码

UIAlertView在IOS 8以上版本已经过时了,官方推荐我们使用UIAlertController代替UIAlertView.UIActionSheet 1?UIAlertController显示普通的Alert - (IBAction)showAlert:(UIButton *)sender { //显示提示框 //过时 // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@&q

ios UIAlertController 的用法

1 UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您确定要删除该商品吗?" preferredStyle:UIAlertControllerStyleAlert]; 2 UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIA

iOS:UIAlertController和UIAlertAction的详解

提示框控制器:UIAlertController 提示框按钮:UIAlertAction 功能:用来提示信息,并给出一些可以进行选择的按钮来处理相应的要求. 注意:在Xcode的iOS8 SDK中,UIAlertView和UIActionSheet都被UIAlertController取代.官方库解释: “UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyle

IOS UIAlertController 弹出框中添加视图(例如日期选择器等等)

UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeDate; UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"\n\n\n\n\n\n\n\n\n\n\n\n" message:nil  preferredStyle:UIAlertContr

IOS开发之UIAlertView与UIAlertController的详尽用法说明

本文将从四个方面对IOS开发中UIAlertView与UIAlertController的用法进行讲解: 一.UIAlertView与UIAlertController是什么东东? 二.我们为什么要用UIAlertView或UIAlertController? 三.如何使用UIAlertView和UIAlertController? 四.阅读提醒. 一.UIAlertView与UIAlertController是什么东东? 一句话,在所有移动应用中,出现的提示窗一般都由UIAlertView或U

IOS 开发 弹框 UIAlertView 与UIAlertController

//View 里面的一个按钮 - (IBAction)showAlert:(id)sender { //ios8 之前 UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"你好,波多老师" message:@"听说您??有新戏要上映了" delegate:nil cancelButtonTitle:@"我来贡献票房来了" otherButtonTitles:nil, nil ]; [a

iOS iOS8新特性-UIAlertController

iOS iOS8新特性--UIAlertController 1. iOS7及iOS7之前警告类控件有UIAlertView和UIActionSheet 1.1 UIAlertView的使用 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"这是一个UIAlertView" delegate:nil cancelButtonTitle:@"取消" oth