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 = 365 * 24 * 60 * 60;
  NSDate *todayDate = [NSDate date];
  NSDate *oneYearFromToday 
    = [todayDate dateByAddingTimeInterval:oneYearTime];
  NSDate *twoYearsFromToday 
    = [todayDate dateByAddingTimeInterval:2 * 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 = 2 * 60;
  [self.myDatePicker setCountDownDuration:twoMinutes];
}

 

Run Result:

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/davidgu/p/3541686.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值