UIDatePicker选中的值是隐藏的

时间:2016-04-27 09:53:13

标签: ios objective-c uidatepicker

我正在以编程方式创建UIDatePicker,允许选择日期(日/月/年)。

当我滚动选择输入(日,月或年)时,始终隐藏所选值。这是一个截图:

UIDatePicker hidden selected value bug

我不知道它是什么类型的错误,并且在Stackoverflow上找不到任何解决方案。

使用的代码:

CGRect datePickerFrame = CGRectMake(0, 40, self.view.frame.size.width, 216);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:datePickerFrame];
[self.view addSubview:datePicker];

iPhone 5S iOS 9.2.1发生此错误 编程语言:Objective-C

2 个答案:

答案 0 :(得分:0)

请尝试以下代码;

    CGRect datePickerFrame = CGRectMake(0, 40, self.view.frame.size.width, 216);
    UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:datePickerFrame];
    datePicker.datePickerMode=UIDatePickerModeDate;
    datePicker.hidden=NO;
    datePicker.date=[NSDate date];
    [datePicker addTarget:self action:@selector(LabelTitle:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];

-(void)LabelTitle:(id)sender
{
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    dateFormat.dateStyle=NSDateFormatterMediumStyle;
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSString *str=[NSString stringWithFormat:@"%@",[dateFormat  stringFromDate:datePicker.date]];
    //assign text to label
    label.text=str; 

答案 1 :(得分:0)

只是通过改变颜色来测试可能是这个问题我不确定

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
 UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
[labelSelected setTextColor:[UIColor redColor]];
}