为UIDatePicker的文本分配标签

时间:2015-05-31 07:14:08

标签: ios objective-c uipickerview uidatepicker

我有一个UIDatePicker,我试图让它水平滚动而不是垂直滚动。我发现this tutorial显示了如何使用 UIPickerView 但不是UIDatePicker来执行此操作。

我遇到的唯一问题是使用以下行:

label.text = [pickerViewArray objectAtIndex:row];

我将label.text =放入什么?我没有array可以assign到'标签'。

这是我从教程中获得的完整方法。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    CGRect rect = CGRectMake(0, 0, 120, 80);
    UILabel *label = [[UILabel alloc]initWithFrame:rect];
    CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
    [label setTransform:rotate];
    label.text = [pickerViewArray objectAtIndex:row];
    label.font = [UIFont systemFontOfSize:22.0];
    label.textAlignment = UITextAlignmentCenter;
    label.numberOfLines = 2;
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.backgroundColor = [UIColor clearColor];
    label.clipsToBounds = YES;
    return label ;
}

1 个答案:

答案 0 :(得分:0)

NSDateFormatter *formatter;

formatter = [[NSDateFormatter alloc] init];

[formatter setDateFormat:@"dd/MM/yyyy"];


yourLabel.text = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[birthdayPicker date]]];

我认为您尝试执行类似 this example

的操作
[self.dateFormatter setDateFormat:@"EE/YYYY"]; // Sun/2015

[self.dateFormatter setDateFormat:@"EE/YY"]; // Sun/15

在这里你可以设置选择器的当前日期和开始/结束日期

[self.dayPicker setStartDate:[NSDate dateFromDay:31 month:5 year:2015] endDate:[NSDate dateFromDay:5 month:6 year:2016]];

[self.dayPicker setCurrentDate:[NSDate dateFromDay:31 month:5 year:2015] animated:NO];

enter image description here