如果我从日期选择器视图中选择日期作为当前日期,时间也会被选中并显示当前时间,我想在两个不同的选择事件中分开
这是我的代码段 -
/ *用于显示UIDatePicker视图的按钮* /
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM, yyyy"];
self.lbl_StartDateValue.text = [dateFormatter stringFromDate:[sender date]];
self.planStartDate = [dateFormatter stringFromDate:[sender date]];
NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:[dateFormatter dateFromString:[dateFormatter stringFromDate:[sender date]]] options:0];
self.lbl_EndDateValue.text = [NSString stringWithFormat:@"End %@",[dateFormatter stringFromDate:date]];
self.planEndDate = [dateFormatter stringFromDate:date];
}
// Here I am not able to differentiate the selection of two picker view
-(void)onDoneButtonClick {
[self dueDateChanged:self.picker];
[self dueTimeChanged:self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[self.tempBackgroundView removeFromSuperview];
}
-(void) dueTimeChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh:mm a"];
self.lbl_timeValue.text = [dateFormatter stringFromDate:[sender date]];
self.planAlarmtime = [dateFormatter stringFromDate:[sender date]];
}
答案 0 :(得分:1)
只需将最低数据设为当前日期
[self.picker setMinimumDate:[NSDate date]];
在滚动Picker
之前,您也不会选择如果要将默认日期显示为当前日期
self.picker.date = [NSDate date]
答案 1 :(得分:1)
这样,当用户出现日期选择器时,最初会在文本字段中设置当前日期
(IBAction)btn_calendar_event:(id)sender {
......
[self.toolbar sizeToFit];
[self.view addSubview:self.toolbar];
//if you want to show the default date as current date
self.picker.date = [NSDate date];
// set inital date on your textfield
[self dueDateChanged : self.picker];
}
如果用户按下完成按钮,则调用此
-(void)onDoneButtonClick {
// call the method
[self dueDateChanged : self.picker];
[self.toolbar removeFromSuperview];
[self.picker removeFromSuperview];
[_txtf_birthDate resignFirstResponder];
}
最后更改YYYY格式
-(void) dueDateChanged:(UIDatePicker *)sender {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSLog(@"Picked the date %@", [dateFormatter stringFromDate:[sender date]]);
_txtf_birthDate.text = [dateFormatter stringFromDate:[sender date]];
}
答案 2 :(得分:0)
UIDatePicker *datepicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-320, self.view.frame.size.width, 320)];
datepicker.date = [NSDate date];
[self.view addSubview:datepicker];