【问题标题】:feting label time on the datepicker日期选择器上的标签时间
【发布时间】:2013-12-06 12:52:22
【问题描述】:

我正在使用 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;
}

下面的图片可以更详细地描述你

【问题讨论】:

    标签: ios iphone datepicker


    【解决方案1】:

    使用 UIDatePicker 的 setDate:animated...

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

    【讨论】:

    • 我已经使用了这个,但它没有显示我想要的结果??
    猜你喜欢
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多