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 ...
随机推荐
- [Duilib] 交替背景色设置失败的原因
用列表显示一列数据时,相邻数据常用不同背景色来达到区别的作用.但是设置了Duilib相应属性之后交替背景色效果并未出现.逐一排除之后发现是item的enable属性设置为"false&quo ...
- C++中string 的使用
string类的构造函数:string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化此外,string类还支持 ...
- java多线程之synchronized(线程同步)
一.线程同步,主要应用synchronized关键字: public class TraditionalThreadSynchorinzed { public static void main(Str ...
- CircleLayout
CircleLayout https://developer.apple.com/library/ios/samplecode/CircleLayout/Introduction/Intro.html ...
- 老 base64 for xe8
not recommend ,only for study procedure TForm1.Button3Click(Sender: TObject); var ssi, sso: TStringS ...
- 触控发布《Cocos开发者平台白皮书》
Cocos 2014 开发者大会(秋季)组委会今天正式发布了<Cocos开发者平台白皮书>,GameRes游资网得到Cocos官方授权发布该白皮书电子版. 白皮书主要内容包括对行业的趋势解 ...
- MyEclipse中消除frame引起的“the file XXX can not be found.Please check the location and try again.”的错误
读者如要转载,请标明出处和作者名,谢谢. 地址01:http://space.itpub.net/25851087 地址02:http://www.cnblogs.com/zjrodger/ 作者名: ...
- 我的第一个CUDA程序
最近在学习CUDA框架,折腾了一个多月终于把CUDA安装完毕,现在终于跑通了自己的一个CUDA的Hello world程序,值得欣喜~ 首先,关于CUDA的初始化,代码和解释如下,这部分主要参考GXW ...
- 泡泡堂、QQ堂游戏通信架构分析
http://blog.csdn.net/sodme/article/details/468327#comments ————————————————————————————————————————— ...
- VoToucher
VoToucher package com.isoftstone.pcis.policy.common.utils; import com.isoftstone.pcis.policy.common. ...