从标签到日期选择器获取日期

时间:2013-12-06 10:14:49

标签: ios iphone datepicker

我正在使用UIdatepicker。我从datepicker中获取时间并在标签上显示日期。一旦我选择了它在标签上显示的日期,就会隐藏日期选择器。现在我想要当我调用选择器时它应该显示我在datepicker上选择的标签日期的日期选择器的选择栏。我正在使用的代码如下所示: -

-(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;
}

2 个答案:

答案 0 :(得分:0)

您希望datepicker显示您已在标签上设置的日期。

为此,您需要使用以下方法。

- (void)setDate:(NSDate *)date animated:(BOOL)animated

将所选日期传递给标签。

答案 1 :(得分:0)

试试这个

   [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];
   }

希望这会有用