zoukankan      html  css  js  c++  java
  • iOS 8 UIAlertController 和 UIAlertAction

    将alertView 和 actionSheet 封装在UIAlertController 里面化整为零,使开发者更便利

    当我们一味的追求高内聚,低耦合的时候,伟大的苹果反其道而行之,这也告诉了我们一个道理:

    只有水平高了,内聚也就高了,耦合度自然就低了!哈哈,废话少说,直接上图:

    这是整个 demo 的效果图:

    下面看看 alert 相关东西的实现,直接上代码:

    - (IBAction)alertAction:(id)sender {

        UIAlertController * alter = [UIAlertController alertControllerWithTitle:@"通知" message:@"请填写登陆信息!"  // 初始化   alert  preferredStyle:UIAlertControllerStyleAlert];

        

        [alter addTextFieldWithConfigurationHandler:^(UITextField *  textField) {                              

      /*注意:

      为 alert 添加textfield 注意 只有preferredStyle 

        为UIAlertControllerStyleAlert时候才能添加textfield

      在回调的block内配置  textfield 

      */ 

            [textField setFrame:CGRectMake(0, 50, 44, 200)];

            [textField setTextColor:[UIColor blueColor]];

            [textField setPlaceholder:@"NAMEFIELD"];

            [textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];

            [textField setBorderStyle:UITextBorderStyleRoundedRect];

        }];

        [alter addTextFieldWithConfigurationHandler:^(UITextField *  textField) {

            [textField setFrame:CGRectMake(0, 100, 60, 200)];

            [textField setTextColor:[UIColor blueColor]];

            [textField setPlaceholder:@"PASSWORD"];

            [textField setSecureTextEntry:YES];

            [textField setClearButtonMode:(UITextFieldViewModeWhileEditing)];

            [textField setBorderStyle:UITextBorderStyleRoundedRect];

            

        }];

        

        [alter addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {

    /*

      在这里添加动作的实现

    */

            NSLog(@"%@---%@",alter.textFields[0].text,alter.textFields[1].text);

        }]];

        [self presentViewController:alter animated:YES completion:nil];     // 显示 alert

    }

    代码撸好了,运行,点击alertAction  啪的一声,出现了如下的效果图

    下面看看actionSheet 的实现  

    - (IBAction)actionSheet:(id)sender {

        UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];

        [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *  action) {

            NSLog(@"---拍照获取!");

        // 再次添加实现

        }]];

        [alert addAction:[UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *  action) {

            NSLog(@"---从相册获取!");

          //在此添加实现

            

        }]];

        

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

    }

     然后运行点击  ,啪的一声:

    更过的进阶技术可以关注公众号:进阶的脚步  回复学习资料  有惊喜哦

  • 相关阅读:
    vue学习简单入门
    Python3基础学习
    MySQL数据库索引详解
    使用nginx部署多个前端项目
    基于SpringBoot2.x和tkMapper快速搭建微服务项目脚手架工程
    安装篇-Linux安装maven3.5.2
    安装篇-安装maven3.6.1
    安装篇-安装Idea2019.3.3
    安装篇-jdk1.8安装
    【错误解决】Intellj(IDEA) warning no artifacts configured
  • 原文地址:https://www.cnblogs.com/feiyafeiblog/p/5043012.html
Copyright © 2011-2022 走看看