IOS UIDatePicker

文章目录


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

在这里插入图片描述
在这里插入图片描述
代码:https://github.com/ShaeZhuJiu/UIDatePicker_base.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值