ios9新增 UIAlertController 简单用法

来源:互联网 发布:linux 安装内核源码 编辑:程序博客网 时间:2024/05/21 22:41
  // 一 .创建 UIalertController继承于UIViewController
    //    1.创建UIAlertController
    //    2.初始化UIAlertController,需要使用alertControllerWithTitle:message:preferredStyle:方法;
    //    3.设置Title 、message、preferredStyle;
    //    4.preferredStyle风格样式有两种:UIAlertControllerStyleAlert和UIAlertControllerStyleActionSheet,是分别代替UIalertView和UIActionSheet的;


    //  第一种  UIAlertControllerStyleAlert(屏幕的中间)
//    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
    //
//    提示框是 模态 产生的
  //  [self presentViewController:alertController animated:YES completion:nil];

    //    使用NSLocalizedString本地化(宏)
    //    第一个参数key是从Localizable.strings这个文件中读取对应的key-value值
    //    第二个参数comment可以是nil,可以是一段为空的字符串,也可以是对key的注释

    //    初始化 的不同方法
    //   preferred [pri'fə:d]  首选的

   //     NSString *title = NSLocalizedString(@"A short is best ", @"标题");
 //      NSString *messge = NSLocalizedString(@"A messgea should be a shaort", nil);
  //      UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:messge preferredStyle:UIAlertControllerStyleAlert];
    //模态
    //    [self presentViewController:alertController animated:YES completion:nil];


    //第二种  UIAlertControllerStyleActionSheet(屏幕的底部)
          UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"标题" message:@"UIAlertController" preferredStyle:UIAlertControllerStyleActionSheet];

    // 二. 显示UIAlertController
    //    1. UIAlertController继承于UIViewController;
    //    2. 显实需要使用UIviewController的方法:presentViewController弹出试图控制器.
    //模态
       [self presentViewController:alertController animated:YES completion:nil];


    //    三 . 添加按钮到UIAlertController
    // 1. 创建UIAlertAction 作为 UIalertController的按钮项;
    // 2. 初始化方法 : actionWithTitle:style:handler: ;
    // 3. 设置Title  style  handler;
    // 4. 添加 UIAlertAction 到 UIAlertController.
    /*
     ******添加按钮
     Title :标题名称
     style :样式[Cancle(取消) Default(默认的) destructive(重置)]
     handler:处理程序(点击按钮执行的代码)
     */

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //添加 点击事件
        self.view.backgroundColor = [UIColor yellowColor];

    }];

    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认default" style:UIAlertActionStyleDefault handler:nil];
    // “警示”样式
    UIAlertAction *destructiveAction = [UIAlertAction actionWithTitle:@"重置deatructive" style:UIAlertActionStyleDestructive handler:nil];
    [alertController addAction:cancelAction];
    [alertController addAction:defaultAction];
    [alertController addAction:destructiveAction];
    //    UIAlertAction 继承于NSObject
    UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
     //   UITextField *login = alertController.textFields[0];
        //                UITextField *passWord = alertController.textFields[1];
        //                [self.view endEditing:YES];

        NSLog(@"密码");

    }];
    // ******  注意:  要把按钮(UIAlertAction)加到alertController上
    [alertController addAction:getAction];


    // Do any additional setup after loading the view.

    //三 . 添加文本输入框
    //1. 文本输入框只能添加到Alert的风格中,ActionSheet是不允许的
    //2.UIAlertController具有只读属性的textFiekds数组,可以按照自己需要的顺序添加;
    //3.添加方式需要block,参数是UItextField;
    //4.添加UItextField监听方法和实现方法

    //添加输入文本输入框,已登录和密码对话框的形式

    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"登陆";
        //    如果要监听UITextFiled开始,结束,改变状态,则需要添加监听代码
        //    1.添加通知,监听textfield内容的改变
        // NSNotificationCenter 是一种 消息通信机制
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
        textField.backgroundColor = [UIColor orangeColor];

    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";  // 占位字
        textField.secureTextEntry = YES; //密文输入
    }];

    //    如果要监听UITextFiled开始,结束,改变状态,则需要添加监听代码
    //    1.添加通知,监听textfield内容的改变
    //    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    //        textField.placeholder = @"添加监听";
    //        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
    //    }];

0 0
原创粉丝点击