标签:
UIDatePicker控件特别类似与UIPickerView控件。主要用来对日期的选择。
1. UIDatePicker的实例化和基本设置
self.myDatePicker = [[UIDatePicker alloc] init];
[self.myDatePicker setCenter:self.view.center];
// 通过setDatePickerMode方法,来设置UIDatePicker的样式。
[self.myDatePicker setDatePickerMode:UIDatePickerModeDate];
// 添加事件
[self.myDatePicker addTarget:self
action:@selector(datePickerDateChanged:)
forControlEvents:UIControlEventValueChanged];
// 设置可以选择的时间区域
NSTimeInterval timeInterval=60*60*24*365;
NSDate *oneYearFromToday = [[NSDate date] dateByAddingTimeInterval:timeInterval];
NSDate *twoYearsFromToday = [[NSDate date] dateByAddingTimeInterval:2 * timeInterval];
[self.myDatePicker setMinimumDate:oneYearFromToday];
[self.myDatePicker setMaximumDate:twoYearsFromToday];
datePickerDateChanged方法:
#pragma mark- UIDatePicker 事件
-(void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{
if ([paramDatePicker isEqual:self.myDatePicker]){
NSLog(@"Selected date = %@", paramDatePicker.date);
}
}
setDatePickerMode方法用来设置UIDatePicker的样式。可用样式为:
typedef NS_ENUM(NSInteger, UIDatePickerMode) {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer
};
来自http://blog.sina.com.cn/s/blog_7b9d64af0101bzd9.html
标签:
原文地址:http://www.cnblogs.com/lvchenhao/p/4242797.html