美文网首页IOS
UIAlertController使用?? UIAlertVie

UIAlertController使用?? UIAlertVie

作者: hello_JSH | 来源:发表于2015-09-20 02:20 被阅读802次

    关于UIAlertController取代 UIAlertView/UIActionSheet相关应用:
    现在简介一下UIAlertController使用/简单的实例:
    开发中遇到的弹框类型有:

    QQ20161027-5.png QQ20161027-4.png QQ20161027-3.png QQ20161027-7.png QQ20161027-6.png

    =======

    - (void) UIAlertController
    {
        // 一、UIAlertController简介
        // 二、实例对比,注意过程。(UIAlertAction *)
        // 三、UIAlertAction作用、理解?
    }
    

    一、UIAlertController简介

    UIAlertController:首先明确它是一种控制器,它存在的目的:将AlertView/ActionSheet两个view 合二为一,将两者的用法同一规整。UIAlertController以一种‘模块化(block)’替换 两者的功能和作用(代理)。

    1.如何解决,什么时候是alertView、什么时候是actionSheet?###

    • UIAlertController类方法创建时,设置preferredStyle参数值控制区分两者:UIAlertControllerStyleActionSheet--actionSheet,UIAlertControllerStyleAlert--alert。

    二、通过UIAlertController、UIActionSheet对比实现下图效果?

    QQ20150920-1@2x.png

    1.ios8-之前代码###

    //若是alertView,UIAlertView中部可以带有输入框的,通过属性alertViewStyle实现
    //枚举值    
    /* 
        UIAlertViewStyleDefault = 0,//默认样式,没有输入框
        UIAlertViewStyleSecureTextInput,//单框_密码输入框样式
        UIAlertViewStylePlainTextInput,//单框_普通输入框样式
        UIAlertViewStyleLoginAndPasswordInput//双框_账户/密码输入框样式
    */
    - (void)initAcitonSheet
    {
            UIActionSheet *warnHUD = [[UIActionSheet alloc] initWithTitle:@"!警告:您真要注销账户吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"呵呵", nil];
            warnHUD.delegate  = self; // 设置代理,处理 提示框内 交互事件
            [warnHUD showInView:self.view]; // 必须调用才能展现出来
    
    }
    
    // 要想处理 提示框内部交互事件:就必须实现代理方法:下面据一方法例子,不在一一列举了。。大家应该都会使用。
    // 重点delegate方法
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        // 确定:0, 其他:1/2/3, 取消:为最大值.
        if (buttonIndex == 0)  // 点击是确定按钮
        {
              [self.navigationController popViewControllerAnimated:YES];
        }
    }
    

    2.ios8+之后代码,UIAlertController使用###

    
    - (void)neetInitActionController
    {
        // UIAlertAction类:下面讲解,这里就先理解为:提示框中,苹果给我封装好的,一种特殊‘按钮’,就可了,block里代码,就是点击了‘按钮’后,要做的事情。
        //Title:展示标题
        //style:样式(枚举值)UIAlertActionStyleDefault:默认样式蓝色值,UIAlertActionStyleCancel:取消模式蓝色值,但是永远展示在最下面/最左边.UIAlertActionStyleDestructive:毁灭性模式红色.
        //注意点:一个UIAlertController对象中,最多只能有一个UIAlertActionStyleCancel模式的UIAlertAction对象.否则报错
        //handler:block 点击后回调
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            // 点击‘取消’,做点事情
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 点击‘确定’,做点事情
            NSLog(@"accountTxf.text==%@",self.accountTxf.text);
        }];
        
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            // 点击‘呵呵’,做点事情
        }];
        
        
        //Style:样式,UIAlertControllerStyleActionSheet:默认底部弹框, UIAlertControllerStyleAlert:中部弹框
        UIAlertController *showAlert = [UIAlertController alertControllerWithTitle:@"!警告:您真要注销账户吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [showAlert addAction:action1];
        [showAlert addAction:action2];
        [showAlert addAction:action3];
        //添加输入框
        //注意: 带有输入框的弹框,只能存在UIAlertControllerStyleAlert样式下,UIAlertControllerStyleActionSheet不允许添加输入框.
        [showAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入账户";
            //设置textField相关属性.
            self.accountTxf = textField;
        }];
        [showAlert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
            textField.placeholder = @"请输入密码";
        }];
        
        [self presentViewController:showAlert animated:YES completion:^{
    //         actionController展示完全后,做点事情
        }];
        
        // UIAlertController 不能自己去展示自己,他是一个Vc,只能通过present..进入栈顶,才能将其view展现出来。
        // 区别:UIAlertView/UIActionSheet,展示方法是自身方法,主动展示。而:UIAlertController是,被别人操作来展示自己。
    }
    

    写了好久了,看着没有任何反馈,没有任何点赞的,,今日加强一下...
    如果使用可将-(void) neetInitActionController方法代码全部拷贝去,自己稍加修改就可使用.....

    相关文章

      网友评论

        本文标题:UIAlertController使用?? UIAlertVie

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