日期选择器(UIDatePicker)

选择日期和时间的空间  也可作为倒计时空间 继承了UIControl 可做为活动空间使用

1.Mode

Date  仅显示日期,不显示时间

Time:仅显示时间,不显示日期

DateandTime:同时显示日期和时间

CountDownTimer:仅显示为倒计时器

2.Locale

的国家化Locale  通常无需手动设置 默认使用iOS系统的国际化Locale

3.Interval

仅当该UIDatePicker控件采用Time,DateandTime 和CountDownTimer三种模式时邮箱,设置空间上两个时间之间的间隔

4.Constraints

最小时间和最大时间

5.Timer

采用CountDownTimer模式时有效,作为倒计时控件时剩下的秒数

日期选择器:

self.dp=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];

self.dp.datePickerMode=UIDatePickerModeDateAndTime;

[self.view addSubview:self.dp];

UIButton* btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

btn.frame=CGRectMake(200, 660, 40, 40);

[btn setTitle:@"确定" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(clicked:) forControlEvents:

UIControlEventTouchUpInside];

[self.view addSubview:btn];

// Do any additional setup after loading the view, typically from a nib.

}

-(void)clicked:(id)sender{

NSLog(@"123");

NSDate* selected=[self.dp date];

NSDateFormatter* dateFormatter=[[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy年MM月dd日 HH:mm+0800"];

NSString* destDateString=[dateFormatter stringFromDate:selected];

NSString* message=[NSString stringWithFormat:@"您选择的日期和时间是:%@",destDateString];

UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"日期和时间" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

}

倒计时器

需要一个NSTimer定时器定期更新控件的剩余时间

最重要的属性为:countDownDuration 该属性代表该倒计时器剩余时间 可修改剩余时间和获取

self.dp=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 20, 400, 500)];

self.dp.datePickerMode=UIDatePickerModeCountDownTimer;

[self.view addSubview:self.dp];

self.btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];

self.btn.frame=CGRectMake(200, 660, 40, 40);

[self.btn setTitle:@"确定" forState:UIControlStateNormal];

[self.btn addTarget:self action:@selector(clicked:) forControlEvents:

UIControlEventTouchUpInside];

[self.view addSubview:self.btn];

// Do any additional setup after loading the view, typically from a nib.

}

-(void)clicked:(id)sender{

self.leftSeconds=self.dp.countDownDuration;

[self.dp setEnabled:NO];

[sender setEnabled:NO];

NSString* message=[NSString stringWithFormat:@"开始倒计时?您还成【%ldd】秒",(long)self.leftSeconds];

UIAlertView* alert=[[UIAlertView alloc]

initWithTitle:@"开始倒计时?" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

self.timer=[NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(tickDown) userInfo:nil repeats:YES];

}

-(void)tickDown{

self.leftSeconds-=60;

self.dp.countDownDuration=self.leftSeconds;

if (self.leftSeconds<=0) {

[self.timer invalidate];

self.dp.enabled=YES;

self.btn.enabled=YES;

}

}

你可能感兴趣的:(日期选择器(UIDatePicker))