• UIAlertController(警告栏) 自学之初体验


    UIAlertController有两种样式  preferredStyle:

    UIAlertControllerStyleAlert (位于屏幕的中部)

    UIAlertControllerStyleActionSheet(位于屏幕的下方)
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"标题" message:@"默罕默德~本拉登" preferredStyle:UIAlertControllerStyleAlert];  //UIAlertController的创建

    UIAlertAction是UIAlertController的按钮样式
    title按钮的名称,style按钮的样式,handler处理层序(点击按钮执行的代码)
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消cancel" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"默认" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *resetAction = [UIAlertAction actionWithTitle:@"重置" style:UIAlertActionStyleDestructive handler:nil];
        //添加按钮到UIAlertController上
        [alert addAction:cancelAction];
        [alert addAction:defaultAction];
        [alert addAction:resetAction];

     1、文本输入框只能添加到Alert的风格中,ActionSheet是不允许的;

     2、UIAlertController具有只读属性的textFields数组,需要可直接按自己需要的顺序添加;

     3、添加方式是使用block,参数是UITextField;

     4、添加UITextField监听方法和实现方法。

        //添加文本输入框
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"登陆";
            //可以为textField添加事件
        }];
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
            textField.placeholder = @"密码";
            textField.secureTextEntry = YES;
            //可以为textField添加事件
        }];

    添加一个事件可以用来输出用户名和密码(textFields是属性,是一个数组)

    UIAlertAction *getAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
                UITextField *login = alert.textFields[0];
                UITextField *passWord = alert.textFields[1];
                NSLog(@"登陆:%@   密码:%@",login.text,passWord.text);
            }];//获取textField的文本内容
            [alert addAction:getAction];


    如果要监听textField开始,结束,改变状态,需要添加监听代码
        [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
                textField.placeholder = @"添加监听代码";
                [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];
            }];
        [self presentViewController:alert animated:YES completion:^{
        }];  //模态推送到页面上

    //监听的方法
    -(void)alertTextFieldDidChange:(NSNotification *)notification{
        UIAlertController *alertController = (UIAlertController *)self.presentedViewController;
        if (alertController) {
            //为textFields数组中下标为2的textField为监听对象
            //也是alertController.textFields.lastObject
            UITextField *listen = alertController.textFields[2];
            //限制,如果listen限制输入长度在5个字符内,否则不允许点击默认Defult键
            //当UITextField输入字数超过5个,按钮变灰色,enable为NO
            UIAlertAction *action = alertController.actions.lastObject;
            action.enabled = listen.text.length<=5;
        }

  • 相关阅读:
    03月03日23:24:40 学习进度笔记
    03月03日23:23:03 学习进度笔记
    03月03日23:19:31 学习进度笔记
    为什么要使用Elasticsearch
    03月03日23:16:59 学习进度笔记
    4
    6
    10
    11
    5
  • 原文地址:https://www.cnblogs.com/LE-Quan/p/5145566.html
Copyright © 2020-2023  润新知