【问题标题】:UIDatePicker Date property won't set with the date selected - rather gives the current dateUIDatePicker Date 属性不会使用所选日期设置 - 而是给出当前日期
【发布时间】:2012-07-20 01:47:06
【问题描述】:

我的应用程序中有一个 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 的输出始终是当前日期,而不是在 UIDatePicker 中选择的日期。不知道为什么会这样,因为据我所知,date 属性应该显示选择的内容,而不是当前日期(或在用户更改值之前最初选择的内容)。

任何帮助将不胜感激。 (如果您需要更多信息,请给我留言)。 谢谢!我敢肯定,这一定是我在匆忙中忽略了一些事情。

【问题讨论】:

    标签: objective-c ios ios5


    【解决方案1】:

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

    我必须修改 pickerChange 函数以包含发件人 - 但是人们期望任何其他类型的对 datePicker 的引用都会产生相同的效果。下面是它现在的样子:

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

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

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

    【讨论】:

      【解决方案2】:

      如果您将UIControlEventValueChanged 更改为UIControlEventEditingDidEnd,会发生什么情况?

      【讨论】:

      • 嗨。谢谢你的帮助。我刚试过这个,结果和以前一样。但是我找到了解决方案(请参阅我刚刚发布的答案),但是对于正在发生的事情来说,这似乎仍然很不寻常。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2016-08-05
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多