UIDatePicker提供了一个快速选择日期和时间的控件,它是UIControl的子类,专门用于日期、时间的选择。其样式可以通过UIDatePicker的属性进行灵活设置,同时我们也可以获取到当前UIDatePicker的值,从而进行下一步的操作。
简介
设置UIDatePicker的展示样式。UIDatePicker支持4种类型的样式,分别为UIDatePickerModeTime(仅显示时间)、UIDatePickerModeDate(仅显示日期)、UIDatePickerModeDateAndTime(显示时间和日期)、UIDatePickerModeCountDownTimer(倒计时)
@property (nonatomic) UIDatePickerMode datePickerMode;
设置地区。地区属性的设置会影响到日期以及时间文字的展示方式。
@property (nullable, nonatomic, strong) NSLocale *locale;
设置时区。
@property (nullable, nonatomic, strong) NSTimeZone *timeZone;
获取UIDatePicker当前的日期/时间值。
@property (nonatomic, strong) NSDate *date;
设置当前UIDatePicker显示的日期/时间。
-(void)setDate:(NSDate *)date animated:(BOOL)animated;
基本使用
一定要用日期格式器的方式获取时间,不然会有bug。
//
// ViewController.m
// UIDatePicker
//
// Created by on 2019/7/18.
// Copyright © 2019 Shae. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UIDatePicker *datePicker;
@property(nonatomic,strong)UIButton *button;
@end
@implementation ViewController
- (UIDatePicker *)datePicker{
if (_datePicker==nil) {
_datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 200, [UIScreen mainScreen].bounds.size.width, 200)];
_datePicker.datePickerMode=UIDatePickerModeDateAndTime;
_datePicker.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
}
return _datePicker;
}
-(UIButton *)button{
if (_button==nil) {
_button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
_button.center=CGPointMake([UIScreen mainScreen].bounds.size.width/2, [UIScreen mainScreen].bounds.size.height/2);
[_button setTitle:@"确定" forState:normal];
[_button addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
_button.backgroundColor=[UIColor redColor];
}
return _button;
}
- (void)clicked{
//获取用户通过UIDatePicker设置的日期和时间
NSDate *selected=[self.datePicker date];
//创建一个日期格式器
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
//为日期格式器设置格式字符串
[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm +0800"];
//使用日期格式器格式化日期和时间
NSString *destDateString=[dateFormatter stringFromDate:selected];
NSString *message=[NSString stringWithFormat:@"您选择的日期是:%@",destDateString];
//警告框
UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"时间" message:message preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:action];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:[self datePicker]];
[self.view addSubview:[self button]];
}
@end