• iOS开发之UIAlertController的适配


    • 在iOS8中,只能用UIAlertController.而原来的UIAlertView及UIActionSheet已经被抛弃掉了.但是如果一台iOS 7 的手机运行到有UIAlertController的程序后,必定会崩掉.所以一般我们都要对UIAlertController进行适配.
    • 不用着急,先看完人家嘛,后面人家已经贴出代码了.就先看完全文呗.
    • 本文将介绍UIAlertController中添加一个UIDatePicker的例子
      先上图
    iOS8 UIActionSheet
    iOS8 UIActionSheet
    ios7  UIActionSheet
    ios7 UIActionSheet

    看完图片的介绍,大概应该能知道做了什么
    1.首先应该定义iOS8,以此来判断是ios7还是ios8
    #define IOS8 [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0
    2.先贴出UIActionSheet的代码

    用法将在代码的中加解释,请注意看(写篇文章也是不容易啊)

    if (IOS8) {
            UIDatePicker *datePicker = [[UIDatePicker alloc] init];
            datePicker.datePickerMode = UIDatePickerModeDate;
    
    //解释1,是用于给UIDatePicker留出空间的,因为UIDatePicker的大小是系统定死的,我试过用frame来设置,当然是没有效果的.
    //还有就是UIAlertControllerStyleActionSheet是用来设置ActionSheet还是alert的
    
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"
    
    
    
    
    
    
    
    
    
    
    
    " message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    //增加子控件--直接添加到alert的view上面
            [alert.view addSubview:datePicker];
    //解释2: handler是一个block,当点击ok这个按钮的时候,就会调用handler里面的代码.
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
                NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
    
                //实例化一个NSDateFormatter对象
                [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式
    
                NSString *dateString = [dateFormat stringFromDate:datePicker.date];
    
                //求出当天的时间字符串
                NSLog(@"%@",dateString);
    
    
            }];
    
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
            }];
    
            [alert addAction:ok];//添加按钮
    
            [alert addAction:cancel];//添加按钮
            //以modal的形式
            [self presentViewController:alert animated:YES completion:^{ }];
        }else{
    //当在ios7上面运行的时候,
            UIDatePicker *datePicker = [[UIDatePicker alloc] init];
            datePicker.datePickerMode = UIDatePickerModeDate;
            //[datePicker addTarget:self action:@selector(timeChange:) forControlEvents:UIControlEventValueChanged];
            datePicker7 = datePicker;
    
            UIActionSheet* startsheet = [[UIActionSheet alloc] initWithTitle:@"
    
    
    
    
    
    
    
    
    
    
    
    "
                                                                    delegate:self
                                                           cancelButtonTitle:nil
                                                      destructiveButtonTitle:nil
                                                           otherButtonTitles:@"确定",
                                         @"取消", nil];
            startsheet.tag = 333;
    //添加子控件的方式也是直接添加
            [startsheet addSubview:datePicker];
            [startsheet showInView:self.view];
        }

    总结区别:

    • 在iOS8中,按钮的点击事件都在初始化的时候给设置了.而在ios7中,则要设置代理,
      - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
      以此来判断按的时哪个按钮,每个按钮要做什么事情都要在这里面设置.这无疑iOS8在这点上就更加方便明了,可读性更高一些了.
    • actionSheet的添加子控件的方式iOS8:[alert.view addSubview:子控件];
      ios7: [startsheet addSubview:子控件];虽然方式有点不一样,但是都比较简单,比较爽.
    iOS8 alert
    iOS8 alert
    ios7 alert.png
    ios7 alert.png

    3.UIAlertView的代码

    if(IOS8){//如果是iOS8
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"什么鬼" message:@"
    
    " preferredStyle:UIAlertControllerStyleAlert];
    //这里就可以设置子控件的frame,但是alert的frame不可以设置
            UITextField * text = [[UITextField alloc] initWithFrame:CGRectMake(15, 64, 240, 30)];//wight = 270;
            text.borderStyle = UITextBorderStyleRoundedRect;//设置边框的样式
    //添加子控件也是直接add,爽
            [alert.view addSubview:text];
    //这跟 actionSheet有点类似了,因为都是UIAlertController里面的嘛
            UIAlertAction *ok = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"%@",text.text);//控制台中打印出输入的内容
            }];
    
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    
            }];
    //添加按钮
            [alert addAction:ok];
            [alert addAction:cancel];
    //以modal的方式来弹出
            [self presentViewController:alert animated:YES completion:^{ }];
        }else{//如果是ios7的话
    
            if (customAlertView==nil) {
                customAlertView = [[UIAlertView alloc] initWithTitle:@"xixi" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            }
            [customAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
            UITextField *nameField = [customAlertView textFieldAtIndex:0];
            nameField.placeholder = @"请输入一个名称";
    
            [customAlertView show];
        }

    AlertView总结区别:

    • iOS8的alertview在中间,而ios7的是偏上一点的.
    • iOS8中的alertView可以加其他的控件,但是ios7上面只能加文本输入框(两种,有密码和非密码的输入框)

    但是两种方法显然还是有些不一样的.那么如果才能做得一样呢.下面提供一种思路

    • 最好就是自定义一个UIView,然后像键盘一样隐藏在最下面,当需要弹框的时候,就直接移上来.如果需要全屏显示,而又有UINavigation的话,就可以modal一个控制器或者在window上加一个UIView[[UIApplication sharedApplication].keyWindow addSubview:<#(UIView *)view#>]
    • 这种方式在demo中也是有的,只是做了个大概,有基础的朋友一定明白了的.

    如果还有需要请到guihit上下载https://github.com/ouzhenxuan/UIAlertControllers
    如果你觉得demo对你有用,请不要手下留情,拼命在guihub上点star(赞)吧.当然简书上也是很欢迎的.

  • 相关阅读:
    golang 操作json
    CloudFoundry应用部署记录
    Stream流
    学做8位计算机
    无废话技术选型之--消息中间件选型(rabbitMQ、rocketMQ、kafka)
    无废话设计模式(22)行为型模式--解释器模式
    无废话设计模式(21)行为模式--迭代器模式
    无废话设计模式(20)行为型模式--职责链模式
    无废话设计模式(19)行为模式--访问者模式
    无废话设计模式(18)行为型模式--命令模式
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/7080816.html
Copyright © 2020-2023  润新知