美文网首页iOS技术收藏控件类将来跳槽用
iOS-个人整理16 - 警示框--UIAlertControl

iOS-个人整理16 - 警示框--UIAlertControl

作者: 简单也好 | 来源:发表于2016-04-13 16:13 被阅读293次

    一、UIAlertController

    alert顾名思义是弹出一个提示框,在某个版本以前一直用的是UIAlertView,它继承的是UIView,现在已经废弃,但仍可以使用
    UIAlertController完全取代了UIAlertView,因为继承于UIViewController,理论上功能更强大一点

    一般情况下,UIAlertController是点击某个按钮后弹出的,我们就可以把UIAlertController的创建写在某个Button的点击方法中
    在某个button的点击方法中,我们这样写:

    1.使用时先初始化,title是显示的标题,message是标题下面的字,详细说明,
    preferredStyle是样式有两种
    UIAlertControllerStyleAlert:中间弹出的提示框
    UIAlertControllerStyleActionSheet:底部弹出的提示框

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

    2.初始化完毕后警示框是没有按钮的,需要单独添加按钮UIAlertAction
    首先初始化一个按钮
    按钮的style有三种:
    UIAlertActionStyleDefault:默认样式
    UIAlertActionStyleCancel:取消按钮
    UIAlertActionStyleDestructive:红字的警示按钮
    按钮的handler属性后面可以跟一个block,用来传值

    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];  
    把alert窗口用模态方法弹出  
    [self presentViewController:alertController animated:YES completion:nil];  
    

    3.再将按钮添加到警示框上面去,把警示框用模态的方法添加到父视图

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

    4.在UIAlertController中我们不但可以添加UIAlertAction按钮,还可以添加文本框
    一般不常用,实现起来一些具体问题也有点绕
    先初始化一个UIAlertController,使用addTextFieldWithConfigurationHandler方法添加textField,这个方法后面跟的参数是block,block带一个UITextField的参数
    这个textField就是添加上去的文本框,可以对它的属性进行设置

    UIAlertController *alertController =   
    [UIAlertController alertControllerWithTitle:@"文本对话框" message:@"登录和密码对话框示例" preferredStyle:UIAlertControllerStyleAlert];  
      
     [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) { textField.text = @"asda";  
        textField.backgroundColor = [UIColor colorWithRed:109/255.0 green:211/255.0 blue:206/255.0 alpha:1];  
            textField.borderStyle = UITextBorderStyleRoundedRect;  
            textField.textAlignment = NSTextAlignmentCenter;  
     }];  
    

    这里可以添加一个方法,对textField上的文字输入进行限制,少于5字符不能点击按钮,
    在这个block中声明一个通知中心,当UITextFieldTextDidChangeNotification发生的时候(也就是textField的内容改变时)
    进入方法验证输入长度够不够

           
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField){  
      
    //NSNotificationCenter defaultCenter通知一件事的改变会触发另一个事件  
      
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertTextFieldDidChange:) name:UITextFieldTextDidChangeNotification object:textField];  
      
     textField.placeholder = @"登录"; }];  
    

    实现一个方法根据textField输入长度是否够,控制按钮是否可点击

    - (void)alertTextFieldDidChange:(NSNotification *)notification{  
        //得到当前推送的AlertViewController  
        UIAlertController *alertController = (UIAlertController *)self.presentedViewController;  
        if (alertController) {  
            //获得AlertController上的文本框  
            UITextField *login = alertController.textFields.firstObject;  
            UIAlertAction *okAction = alertController.actions.lastObject;  
            okAction.enabled = login.text.length > 5;  
        }  
    }  
    

    下面我们展示几个实例
    这是UIAlertControllerStyleAlert类型的,上面添加了一个cancel按钮和一个destruct按钮

    这是UIAlertControllerStyleActionSheet的样子

    这是添加了文本框的样子


    相关文章

      网友评论

        本文标题:iOS-个人整理16 - 警示框--UIAlertControl

        本文链接:https://www.haomeiwen.com/subject/dulwlttx.html