IOS開發(4)之UIDatePicker控制項

來源:互聯網
上載者:User

1 前言
UIDatePicker是一個十分類似於UIPickerView的類,十分常用,今天我們來學習一下。


2 UIDatePicker簡介
上代碼
日期選擇:
.h檔案:
[plain]
@property(nonatomic,strong) UIDatePicker *myDatePicker; 

@property(nonatomic,strong) UIDatePicker *myDatePicker;
.m檔案:
[plain]
@synthesize myDatePicker; 
 
- (void)viewDidLoad 

    //一年的秒數 
    float oneYearTime = 60*60*24*365; 
    NSLog(@"oneYearTime====>%f",oneYearTime); 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    //事件選取器 
    self.myDatePicker = [[UIDatePicker alloc] init]; 
    self.myDatePicker.center = self.view.center; 
    self.myDatePicker.datePickerMode=UIDatePickerModeDate; 
    //添加事件 
    [self.myDatePicker addTarget:self action:@selector(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged]; 
    //獲得目前時間 
    NSDate *currentDate = self.myDatePicker.date; 
    NSLog(@"Date = %@",currentDate); 
    NSDate *oneYearFromToday = [currentDate dateByAddingTimeInterval:oneYearTime]; 
    NSDate *twoYearFromToday = [currentDate dateByAddingTimeInterval:2*oneYearTime]; 
    //最小可選時間 
    self.myDatePicker.minimumDate = oneYearFromToday; 
    self.myDatePicker.maximumDate = twoYearFromToday; 
    [self.view addSubview:self.myDatePicker]; 

 
-(void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{ 
    if ([paramDatePicker isEqual:self.myDatePicker]) { 
        NSLog(@"Selected date=%@",paramDatePicker); 
    } 

@synthesize myDatePicker;

- (void)viewDidLoad
{
    //一年的秒數
    float oneYearTime = 60*60*24*365;
    NSLog(@"oneYearTime====>%f",oneYearTime);
    [super viewDidLoad];
 // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //事件選取器
    self.myDatePicker = [[UIDatePicker alloc] init];
    self.myDatePicker.center = self.view.center;
    self.myDatePicker.datePickerMode=UIDatePickerModeDate;
    //添加事件
    [self.myDatePicker addTarget:self action:@selector(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged];
    //獲得目前時間
    NSDate *currentDate = self.myDatePicker.date;
    NSLog(@"Date = %@",currentDate);
    NSDate *oneYearFromToday = [currentDate dateByAddingTimeInterval:oneYearTime];
    NSDate *twoYearFromToday = [currentDate dateByAddingTimeInterval:2*oneYearTime];
    //最小可選時間
    self.myDatePicker.minimumDate = oneYearFromToday;
    self.myDatePicker.maximumDate = twoYearFromToday;
    [self.view addSubview:self.myDatePicker];
}

-(void) datePickerDateChanged:(UIDatePicker *)paramDatePicker{
    if ([paramDatePicker isEqual:self.myDatePicker]) {
        NSLog(@"Selected date=%@",paramDatePicker);
    }
}運行效果:



時間選擇:
.m檔案:
[plain]
 (void)viewDidLoad 

    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    self.view.backgroundColor = [UIColor whiteColor]; 
    //事件選取器 
    self.myDatePicker = [[UIDatePicker alloc] init]; 
    self.myDatePicker.center = self.view.center; 
    //設定成時間模式 
    self.myDatePicker.datePickerMode=UIDatePickerModeCountDownTimer; 
    [self.view addSubview:self.myDatePicker]; 
    NSTimeInterval twoMinutes = 2*60; 
    [self.myDatePicker setCountDownDuration:twoMinutes]; 

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //事件選取器
    self.myDatePicker = [[UIDatePicker alloc] init];
    self.myDatePicker.center = self.view.center;
    //設定成時間模式
    self.myDatePicker.datePickerMode=UIDatePickerModeCountDownTimer;
    [self.view addSubview:self.myDatePicker];
    NSTimeInterval twoMinutes = 2*60;
    [self.myDatePicker setCountDownDuration:twoMinutes];
}UIDatePickerMode類型:
[plain]
-type enmu{ 
    UIDatePickerModeTime,//時間 
    UIDatePickerModeDate,//日期 
    UIDatePickerModeDateAndTime,//時間和日期 
    UIDatePickerModeCountDownTimer//倒計時模式 

-type enmu{
    UIDatePickerModeTime,//時間
    UIDatePickerModeDate,//日期
    UIDatePickerModeDateAndTime,//時間和日期
    UIDatePickerModeCountDownTimer//倒計時模式
}運行結果:

 
 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.