UIDatePicker Date属性不会设置选择日期 - 而是提供当前日期

时间:2012-07-20 01:47:06

标签: objective-c ios ios5

我的应用程序中有一个UIDatePicker,虽然我更改了模拟器中选择的日期,当我NSLog datePicker.date时,它会输出当前日期而不是我选择的日期。

以下是我的代码摘录:(生成选择器的地方)

self.pickerView = [[UIDatePicker alloc] initWithFrame:pickerFrame];
//self.pickerView.frame = pickerFrame;

self.pickerView.datePickerMode = UIDatePickerModeDate;

actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                         delegate:nil
                                                cancelButtonTitle:nil
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:nil];

[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

[actionSheet addSubview:pickerView];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];

[closeButton addTarget:self action:@selector(dismissActionSheet) forControlEvents:UIControlEventValueChanged];

[pickerView addTarget:self action:@selector(pickerChange) forControlEvents:UIControlEventValueChanged];

[actionSheet addSubview:closeButton];
[actionSheet setBounds:CGRectMake(0, 0, 320, 251)];

pickerView.maximumDate = [NSDate dateWithTimeIntervalSinceNow:315360000];
pickerView.minimumDate = [NSDate dateWithTimeIntervalSinceNow:-315360000];

然后在我的 pickerChange 函数中:

-(void)pickerChange{
    NSLog(@"DEBUG: Picker Value Changed");
    NSLog(@"%@",self.pickerView.date );
}

该NSLog的输出始终是CURRENT DATE,而不是UIDatePicker中选择的日期。不知道为什么会这样,因为据我所知,date属性应显示所选内容,而不是当前日期(或用户更改值之前最初选择的内容)。

任何帮助将不胜感激。 (如果您需要更多信息,请给我留言)。 谢谢!我确信它一定是我在匆忙中忽略的东西。

2 个答案:

答案 0 :(得分:2)

确定。我设法找到答案 - 我发布它,以便如果其他人有类似的问题,他们可以解决它。

我必须修改pickerChange函数以包含发件人 - 但是可以预期,对datePicker的任何其他类型的引用都会产生相同的效果。现在看来是这样的:

-(void)pickerChange:(UIDatePicker*) datePicker{
    myDate = datePicker.date;
}

似乎这是解决问题的唯一方法,而不是引用'pickerView'(在头文件中声明),你必须以这种方式引用发件人。

看看是否有其他人遇到过这类问题(或类似问题)会很有趣。

答案 1 :(得分:0)

如果您将UIControlEventValueChanged更改为UIControlEventEditingDidEnd会怎样?

相关问题