IOS 7 Study - UIDatePicker
Picking the Date and Time with UIDatePicker
effect:

1. declaring a property of type UIDatePicker
#import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIDatePicker *myDatePicker; @end @implementation ViewController ...
2. instantiate the date picker
- (void)viewDidLoad{
[super viewDidLoad];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
[self.view addSubview:self.myDatePicker];
}
3. add receiver target
Just like the UISwitch class, a date picker sends action messages to its targets whenever
the user has selected a different date. To respond to these messages, the receiver must
add itself as the target of the date picker, using the addTarget:action:forControlEvents
- (void)viewDidLoad{
[super viewDidLoad];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
[self.view addSubview:self.myDatePicker];
[self.myDatePicker addTarget:self
action:@selector(datePickerDateChanged:)
forControlEvents:UIControlEventValueChanged];
}
4. accessing the date
// you can attempt to retrieve its currently selected date using its date property
NSDate *currentDate = self.myDatePicker.date;
NSLog(@"Date = %@", currentDate);
// every time the user changes the date, get a message from the date picker
- (void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{
if ([paramDatePicker isEqual:self.myDatePicker]){
NSLog(@"Selected date = %@", paramDatePicker.date);
}
}
A date picker also lets you set the minimum and the maximum dates that it can display.
For this, let’s first switch our date picker mode to UIDatePickerModeDate and then,
using the maximumDate and the minimumDate properties, adjust this range:
- (void)viewDidLoad{
[super viewDidLoad];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
self.myDatePicker.datePickerMode = UIDatePickerModeDate;
[self.view addSubview:self.myDatePicker];
NSTimeInterval oneYearTime = * * * ;
NSDate *todayDate = [NSDate date];
NSDate *oneYearFromToday
= [todayDate dateByAddingTimeInterval:oneYearTime];
NSDate *twoYearsFromToday
= [todayDate dateByAddingTimeInterval: * oneYearTime];
self.myDatePicker.minimumDate = oneYearFromToday;
self.myDatePicker.maximumDate = twoYearsFromToday;
}
If you want to use the date picker as a countdown timer, you must set your date picker
mode to UIDatePickerModeCountDownTimer and use the countDownDuration property
of the date picker to specify the default countdown duration.
- (void)viewDidLoad{
[super viewDidLoad];
self.myDatePicker = [[UIDatePicker alloc] init];
self.myDatePicker.center = self.view.center;
self.myDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
[self.view addSubview:self.myDatePicker];
NSTimeInterval twoMinutes = * ;
[self.myDatePicker setCountDownDuration:twoMinutes];
}
Run Result:

IOS 7 Study - UIDatePicker的更多相关文章
- iOS开发中UIDatePicker控件的使用方法简介
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Timer四 ...
- IOS 7 Study - UIViewController
Presenting and Managing Views with UIViewController ProblemYou want to switch among different views ...
- iOS学习之UIDatePicker控件使用
iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. , 您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Ti ...
- IOS 7 Study - Displaying an Image on a Navigation Bar
ProblemYou want to display an image instead of text as the title of the current view controlleron th ...
- IOS 7 Study - Manipulating a Navigation Controller’s Array of View
ProblemYou would like to directly manipulate the array of view controllers associated with aspecific ...
- IOS 7 Study - Implementing Navigation with UINavigationController
ProblemYou would like to allow your users to move from one view controller to the other witha smooth ...
- IOS 7 Study - UIActivityViewController(Presenting Sharing Options)
You want to be able to allow your users to share content inside your apps with theirfriends, through ...
- IOS 7 Study - UISegmentedControl
You would like to present a few options to your users from which they can pick anoption, through a U ...
- 解析iOS开发中的FirstResponder第一响应对象
1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...
随机推荐
- 读取Assets中的文件数据
try { // 返回的字节流 InputStream is = getResources().getAssets().open("info.txt"); // 当读取时,属于文本 ...
- bat 批处理脚本
目录: 1:ping多个不同服务器IP 2:每隔一段时间清一次DNS缓存 3:将一个文件夹中的所有文件,分别保存在一个新文件夹中,以保持每个文件夹一个文件 功能1:ping多个不同服务器IP 环境开通 ...
- 推荐一个网站Stack Overflow
网站URL:http://stackoverflow.com 我是怎么知道这个网站的呢?其实这个网站非常出名的,相信许多人都知道.如果你不知道,请继续阅读: 一次我在CSDN上面提问,但是想要再问多几 ...
- Typecho集成ueditor笔记
前言:萝卜青菜各有所爱,因为个人需求所以需要在博客中集成ueditor,并非是我不喜欢md语法 其实本篇的笔记的书写最早也是在本地的md编辑器上完成的 1. 首先下载ueditor编辑器,然后重命名文 ...
- Linux Shell脚本教程
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- java 编写hadoop程序中使用第三方libxx.so库
在使用java编写hadoop处理程序时遇到了,java使用依赖的第三方libxx.so库的情况,找到了一种可行的方法,记录一下,希望对别人也有帮助: 加入需要使用的lib库为libxxx.so 1. ...
- bzoj2940: [Poi2000]条纹
2940: [Poi2000]条纹 条纹游戏是一个双人的游戏.所需要的物品有一个棋盘以及三种颜色的长方形条纹,这三种颜色分别是红色.绿色和蓝色.所有的红色条纹的尺寸是c*1,所有的绿色条纹的尺寸是z* ...
- Strider-test 相关配置
package.json { "name": "test-node", "version": "0.0.0", &quo ...
- SvUDID实现设备唯一标示
//SvUDIDTools : https://github.com/smileEvday/SvUDID //将生成的UDID保存到钥匙串中,用户卸载app再重新安装UDID也不会改变. /* 用法1 ...
- SCAU 13校赛 17115 ooxx numbers
17115 ooxx numbers 时间限制:1000MS 内存限制:65535K 题型: 编程题 语言: 无限制 Description a number A called oo numbe ...