码迷,mamicode.com
首页 > 其他好文 > 详细

UIDatePicker的使用

时间:2015-01-22 23:08:16      阅读:2932      评论:0      收藏:0      [点我收藏+]

标签:

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

 


 

UIDatePicker的使用

标签:

原文地址:http://www.cnblogs.com/lvchenhao/p/4242797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!