• UIDatePicker基本使用


    UIDatePicker提供了一个快速选择日期和时间的控件,他是UIControl的子类,专门用于日期时间的选择。其样式可以通过UIDatePicker的属性进行灵活设置,同时也可以获取到当前UIDatePicker的值。UIDatePicker有几个方法和属性需要重点掌握。

    @property (nonatomic) UIDatePickerMode datePickerMode; //设置UIDatePicker的展示样式,有四种样式

        UIDatePickerModeTime,           // 仅显示时间
        UIDatePickerModeDate,           // 显示年月日
        UIDatePickerModeDateAndTime,    // 显示年月日和时间
        UIDatePickerModeCountDownTimer  //倒计时


    @property (nullable, nonatomic, strong) NSLocale   *locale;   //设置地区,地区的设置会影响日期以及时间文字的展示方式

    @property (nullable, nonatomic, strong) NSTimeZone *timeZone; //设置时区


    @property (nonatomic, strong) NSDate *date; //获取当前日期/时间值

    - (void)setDate:(NSDate *)date animated:(BOOL)animated;//设置当前显示的日期时间

    下面来看一个完整示例,选择时间后点击确定按钮会弹出提示框,提示框内容为选择的时间

    #import "ViewController.h"
    
    @interface ViewController ()
    @property(nonatomic,strong)UIDatePicker *datePicker;
    @property(nonatomic,strong)UIButton *button;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [self.view addSubview:self.datePicker];
        [self.view addSubview:self.button];
    }
    
    //懒加载
    - (UIDatePicker *)datePicker{
        if (_datePicker == nil) {
            self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 280, self.view.frame.size.width, 200)];
            _datePicker.datePickerMode = UIDatePickerModeDateAndTime;
            _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
        }
        return _datePicker;
    }
    
    //懒加载按钮,用于展示datePicker选中的时间
    - (UIButton *)button{
        if (_button == nil) {
            self.button = [[UIButton alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 50) / 2.0, 100, 50, 30)];
            [_button setTitle:@"确定" forState:UIControlStateNormal];
            _button.backgroundColor = [UIColor blueColor];
            [_button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
        }
        return _button;
    }
    
    //确定按钮被点击
    - (void)clickButton{
        //获取用户通过UIDatePicker设置的时间
        NSDate *date = [self.datePicker date];
        
        //创建一个日期格式器
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
        //为日期格式器设置格式字符串
        [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm +0800"];
        //使用日期格式器格式化日期和时间
        NSString *dateString = [dateFormatter stringFromDate:date];
        NSString *message = [NSString stringWithFormat:@"您选择的日期和时间是:%@",dateString];
        
        //警告框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"日期和时间" message:message preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    @end

    运行截图:

  • 相关阅读:
    查看Java中每个数据类型所占的位数,和表示数值的范围,并得出结论。
    同名变量的屏蔽原则
    反码,补码,原码
    机器学习概念性知识总结
    图的最短路径问题
    System Design 笔记及代码(系统设计)
    2016网易游戏实习算法题解(今年找暑期实习的时候参加的)
    Google java style
    18.1---不用加号的加法(CC150)
    Java模板模式(template)
  • 原文地址:https://www.cnblogs.com/li-wei203/p/10804302.html
Copyright © 2020-2023  润新知