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. JVM内存结构之三--持久代

    本文会介绍一些JVM内存结构的基本概念,然后很快会讲到持久代,来看下Java SE 8发布后它究竟到哪去了. 基础知识 JVM只不过是运行在你系统上的另一个进程而已,这一切的魔法始于一个java命令. ...

  2. Golang几个常用记录日志对比

    go语言有一个标准库,log,提供了最基本的日志功能,但是没有什么高级的功能,如果需要高级的特性,就需要使用第三方包,下面是一些候选的包: go_tmlog https://code.google.c ...

  3. Android百度地图开发(一)环境搭建

    1.百度地图官方API文档下载 版本 使用说明 下载 Android SDK 通用资源下载 <离线地图>提供新版离线地图(百度矢量地图)与旧版离线地图(百度栅格地图)下载. 备注: 自An ...

  4. eclipse手动导入dtd文件

    1.在eclipse的工具栏上按照“Window->Preferences->XML->XML Catalog”依次点下去,然后在右侧选中 User Specified Entrie ...

  5. java中的类实现comparable接口 用于排序

    import java.util.Arrays; public class SortApp { public static void main(String[] args) { Student[] s ...

  6. QS之warning message

    Multiple message categories are specified as a comma separated list.

  7. hadoop 异常及处理总结-01(小马哥-原创)

    试验环境: 本地:MyEclipse 集群:Vmware 11+ 6台 Centos 6.5 Hadoop版本: 2.4.0(配置为自动HA) 试验背景: 在正常测试MapReduce(下简称MR)程 ...

  8. Hadoop的分布模式安装

      1.确定集群的结构 IP(主机名) 角色 192.168.1.220(hadoop0) NameNode.JobTracker 192.168.1.221(hadoop1) SecondaryNa ...

  9. Xcode集成Google Test

    Xcode集成Google Test 1.下载源代码https://github.com/google/googletest 2.进入下载文件目录下googletest/make中运行make gte ...

  10. js运动 九宫格展开

    <!doctype html> <html> <head> <meta charset = "utf-8"> <title&g ...