我正在使用 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;
}
下面的图片可以更详细地描述你
最佳答案
使用 setDate:animated of UIDatePicker...
- (void)setDate:(NSDate *)date animated:(BOOL)animated
将 label.text 转换为 NSDate:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"hh:mm"]; // depends on 12(hh) or 24(HH)
NSDate *yourDate = [dateFormatter dateFromString:label.text];
关于ios - 在日期选择器上标记时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20424409/