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的更多相关文章

  1. iOS开发中UIDatePicker控件的使用方法简介

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. 您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Timer四 ...

  2. IOS 7 Study - UIViewController

    Presenting and Managing Views with UIViewController ProblemYou want to switch among different views ...

  3. iOS学习之UIDatePicker控件使用

    iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式. ,   您可以选择自己需要的模式,Time, Date,Date and Time  , Count Down Ti ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. IOS 7 Study - UISegmentedControl

    You would like to present a few options to your users from which they can pick anoption, through a U ...

  9. 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...

随机推荐

  1. RESTful HTTP的实践

    REST是一种风格,而不是标准.因为既没有REST RFC,也没有REST协议规范或者类似的规定.REST架构是Roy Fielding(他也是HTTP和URI规范的主要作者之一)在一篇论文中描述的. ...

  2. nginx upstream的分配方式

    1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. 2.weight 指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况. 例 ...

  3. Code Hard or Go Home

    介绍Webkit的渊源  http://hypercritical.co/2013/04/12/code-hard-or-go-home

  4. 使用curl操作openstack swift

    openstack官网有专门的开发者文档介绍如何使用curl操作swift(http://docs.openstack.org/api/openstack-object-storage/1.0/con ...

  5. stardict dict

    stardict在sourceforge项目里的词典都不见,估计是由于版权方面的问题导致的,不过以前那些还是可以继续用的,没有下载的可以备份一份.每个字典文件夹里都有一个.ifo文件,可以用记事本打开 ...

  6. VHDL之Port map and open

    编SPI的master控制器,使用公司基本的元件,有些端口用不着,恰巧好二哥(不知年龄的数字组组长,本名Holger)来了,于是请教之,告曰open关键词.后来深感自己VHDL水平太水,下了一本电子书 ...

  7. MySql 5.6 慢查询

    网上都巨坑 最后在官网找到了开启方法 原来是配置文件改了 Updated example for 2015 MySQL 5.6: slow_query_log = 1slow_query_log_fi ...

  8. logback打印不出日志

    原因: 1. 可能是jar包不完整 , 基本jar包包括:  logback-access , logback-core , logback-classic , perf4j , slf4j-api ...

  9. JavaScript 链式结构序列化详解

    一.概述 在JavaScript中,链式模式代码,太多太多,如下: if_else: if(...){ //TODO }else if(...){ //TODO }else{ //TODO } swi ...

  10. 第二百二十四天 how can I 坚持

    实物商品兑换,有点小难搞,其实也没什么难的,也就那些东西,不过好像这就是我设计实现的,干起来挺来劲的. 供暖了,挺暖和的,哈哈. 小米耳机(炫彩版)到了.感觉挺好的. 还在纠结到底要买哪种颜色的羽绒服 ...