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(, , self.view.frame.size.width, )];
_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 - ) / 2.0, , , )];
[_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

运行截图:

UIDatePicker基本使用的更多相关文章

  1. UI--UIPickerView和UIDatePicker的总结

    回到顶部 UIPickerView的主要方法和城市选择器的修正 UIPickerView只有两个数据源方法.要想完整地显示出PickerView,需要结合使用代理方法 数据源方法: // 一共有多少组 ...

  2. iOs基础篇(二十二)—— UIPickerView、UIDatePicker控件的使用

    一.UIPickerView UIPickerView是一个选择器控件,可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活. 1.常用属性 (1)num ...

  3. UIDatePicker和UIToolbar的使用

    功能,用UIDatePicker 和UIToolbar 实现点击文本框弹出日期选择空间. 点击确定按钮获取时间显示到对应的Text Field里面,点击取消按钮隐藏键盘. 1.创建textField控 ...

  4. 自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)

    HooDatePicker 介绍(introduction) ==================================================项目需要一个DatePicker,只显 ...

  5. iOS开发--UIDatePicker

    UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...

  6. IOS第11天(4:UIDatePicker时间选择,和键盘处理,加载xib文件,代理模式)

    ***控制层 #import "ViewController.h" #import "CZKeyboardToolbar.h" @interface ViewC ...

  7. UIDatePicker 日期/时间选取器(滚轮)—IOS开发

    UIDatePicker 是一个控制器类,封装了 UIPickerView,但是他是UIControl的子类,专门用于接受日期.时间和持续时长的输入.日期选取器的各列会按照指定的风格进行自动配置,这样 ...

  8. UIDatePicker的用法

    目录[-] 1.Locale 2.Calendar 3.timeZone 4.date 5.minimumDate 6.maximumDate 7.countDownDuration 8.minute ...

  9. UIkit框架之UIDatePicker

    1.继承链:UIcontrol:UIview:UIResponder:NSOobject 2.和uidatepicker相关联的触发事件是 UIControlEventValueChanged,当使用 ...

  10. Objective-c——UI进阶开发第一天(UIPickerView和UIDatePicker)

    一.知识点 1.介绍数据选择控件UIPickerView和日期选择控件UIDatePicker控件 * UIPickerView的案例 * 点餐系统 * 城市选择 * 国旗选择 * UIDatePic ...

随机推荐

  1. USACO Clumsy Cows

    洛谷 P3056 [USACO12NOV]笨牛Clumsy Cows 洛谷传送门 JDOJ 2323: USACO 2012 Nov Silver 1.Clumsy Cows JDOJ传送门 Desc ...

  2. 1. vue 的安装

    兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMAScript 5 的浏览器. 安装: 1.直接用 < ...

  3. [Algorithm] 202. Happy Number

    Write an algorithm to determine if a number is "happy". A happy number is a number defined ...

  4. Windbg的快捷键

    窗口切换 可以使用以下键盘快捷方式窗口之间进行切换. 项 效果 CTRL+TAB 调试信息窗口之间切换. 通过重复使用此密钥,你可以扫描通过的所有窗口,而不考虑是否浮动. 停靠本身,或选项卡式停靠窗口 ...

  5. virtualbox安装问题总结

    还是老问题 重点重点: https://blog.csdn.net/Loisleen/article/details/84975165#1install_the_gcc_make_perl_packa ...

  6. Spring 事物隔离级别,事物传播行为

    Spring 框架中对于事物的管理,主要定义了一下四种属性: 事物的隔离(Isolation)级别 事物的传播行为(Propagation Behavior) 事物的超时时间(TImeout) 是否为 ...

  7. 这样配置:让你的 IDEA 好用到飞起来

    阅读本文大概需要 7 分钟. 来源:blog.csdn.net/fly910905/article/details/77868300 1.设置maven 1.在File->settings-&g ...

  8. java dump 内存分析 elasticsearch Bulk异常引发的Elasticsearch内存泄漏

    Bulk异常引发的Elasticsearch内存泄漏 2018年8月24日更新: 今天放出的6.4版修复了这个问题. 前天公司度假部门一个线上ElasticSearch集群发出报警,有Data Nod ...

  9. 小米win10+kali 双系统

    1.下载kali linux 系统镜像,用windisk32imager 制作启动盘,制作好后千万不要格式化u盘,其他的启动盘制作工具不好用,无法加载系统镜像 2.将u盘插入电脑,重启,电脑重启时按 ...

  10. mknod命令的使用

    1.mknod命令 在Linux系统下,mknod命令可用于系统下字符设备文件和块设备文件的创建. (1)命令语法 mknod(选项)(参数) (2)常用选项说明 -Z:设置安全的上下文. -m:设置 ...