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. 小学四则运算口算练习app---No.6

    今天主要解决按钮的闪退问题以及答案页面的设置: (位置问题还是无能为力....) 除此之外加了一些菜单键,右上角 resultActivity.class ; String select=;i< ...

  2. 积性函数与Dirichlet卷积

    转载自https://oi-wiki.org/math/mobius/ 积性函数 定义 若 $gcd(x,y)=1$ 且 $f(xy)=f(x)f(y)$,则 $f(n)$ 为积性函数. 性质 若 $ ...

  3. 【java异常】Building workspace has encountered a problem. Error

    可能是workspace设置错误,检查一下 或者把项目重新下一下,或者重新maven导入

  4. sqlyog 下载

    https://www.cnblogs.com/givemelove/p/7777975.html

  5. flask静态文件

    Flask 静态文件 Web应用程序通常需要静态文件,例如javascript文件或支持网页显示的CSS文件.通常,配置Web服务器并为您提供这些服务,但在开发过程中,这些文件是从您的包或模块旁边的s ...

  6. LOJ6115 汇合 树上分块

    本题空间很小,那些O(nlogn)的树上lca算法在这里不顶用了,可以考虑树分块. 本题的树分块是基于深度的,即按深度每\(\sqrt n\)分一块,然后一块一块往上跳,一直跳到lca处. 对于这题, ...

  7. ssl证书转换cer转pem

    .pem证书转.cer证书 openssl x509 -outform der -in demo.pem -out demo.cer .cer证书转.pem证书 openssl x509 -infor ...

  8. [BZ1925] [SDOI2010]地精部落

    [BZ1925] [SDOI2010]地精部落 传送门 一道很有意思的DP题. 我们发现因为很难考虑每个排列中的数是否使用过,所以我们想到只维护相对关系. 当我们考虑新的一个位置时,给新的位置的数分配 ...

  9. c++功能与方法笔记

    1. windows判断创建目录 #include <Shlwapi.h> #pragma comment(lib, "shlwapi.lib") //windows ...

  10. asbDio (asbDio.dll)

    using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices ...