//只是簡單的使用,以后我會更新一個日常使用的一些demo
// ViewController.m
// UIDatePickerAll
#import "ViewController.h"
@interface ViewController ()
{
UIDatePicker *myDatePicker;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
myDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 300)];
// 設置日期選擇控件的地區
[myDatePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
// [myDatePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_SC"]];
//默認為當天。
[myDatePicker setCalendar:[NSCalendar currentCalendar]];
// 設置DatePicker的時區。
// 默認為設置為:[datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
// 設置DatePicker的日期。
// 默認設置為:
[myDatePicker setDate:[NSDate date]];
// minimumDate設置DatePicker的允許的最小日期。
// maximumDate設置DatePicker的允許的最大日期
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:10];//設置最大時間為:當前時間推后10天
NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[comps setDay:0];//設置最小時間為:當前時間
NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
[myDatePicker setMaximumDate:maxDate];
[myDatePicker setMinimumDate:minDate];
// 設置DatePicker的倒計時間.
// 1) 設置日期選擇的模
// [myDatePicker setDatePickerMode:UIDatePickerModeCountDownTimer];
// 2) 設置倒計時的時長
// 注意:設置倒計時時長需要在確定模式之后指定
// 倒計時的時長,以秒為單位
// [myDatePicker setCountDownDuration:10 * 60];
//顯示小時,分鍾和AM/PM
// [myDatePicker setDatePickerMode:UIDatePickerModeTime];
//顯示年月日
// [myDatePicker setDatePickerMode:UIDatePickerModeDate];
//顯示小時和分鍾
// [myDatePicker setDatePickerMode:UIDatePickerModeCountDownTimer];
//監聽datepicker值的改變
[myDatePicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
[self.view addSubview:myDatePicker];
}
- (void)dateChange:(UIDatePicker *)date
{
NSLog(@"%@", date.date);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end