[英]Getting date from the label onto the datepicker
我正在使用UIdatepicker。 我正在从日期选择器中获取时间,并在标签上显示日期。 一旦选择日期,标签上就会显示日期,而日期选择器将被隐藏。 现在我想当我打电话给选择器时,应该在我之前在日期选择器上选择的标签的日期向我显示日期选择器的选择栏。我正在使用的代码如下所示:-
-(void)datepickershown:(UILabel *)time animated:(BOOL)animated
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"Select the Time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"DONE" otherButtonTitles:Nil, nil];
pickdate = [[UIDatePicker alloc] initWithFrame:[actionsheet bounds]];
pickdate.hidden = NO;
//pickdate.date = [NSDate date];
pickdate.datePickerMode = UIDatePickerModeTime;
[pickdate addTarget:self action:@selector(changeDateInLabel:) forControlEvents:UIControlEventValueChanged];
[actionsheet addSubview:pickdate];
[actionsheet showInView:self.view];
[actionsheet setBounds:CGRectMake(0,0,320, 500)];
CGRect pickerRect = pickdate.bounds;
pickerRect.origin.y = -90;
pickdate.bounds = pickerRect;
}
- (IBAction)changeDateInLabel:(id)sender
{
NSLog(@"I Am Changing the values");
df = [[NSDateFormatter alloc] init];
df.timeStyle = NSDateFormatterShortStyle;
}
您希望日期选择器显示您已经在标签上设置的日期。
为此,您需要使用以下方法。
- (void)setDate:(NSDate *)date animated:(BOOL)animated
传递日期,无论您选择什么,并显示在标签中。
尝试这个
[self.datePicker setDatePickerMode:UIDatePickerModeDate];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd-MM-yyyy"];
if([dateFormat dateFromString:yourDateLabel.text])
{
NSDate *date = [dateFormat dateFromString:yourDateLabel.text];
[self.datePicker setDate:date];
}
希望这会工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.