UIDatePicker 默认返回 Null?

Posted

技术标签:

【中文标题】UIDatePicker 默认返回 Null?【英文标题】:UIDatePicker Returns Null By Default? 【发布时间】:2011-08-27 04:56:22 【问题描述】:

我对 UIDatePicker 的实现本质上是 this one。

我的问题是,如果 UIDatePicker 弹出并且用户没有更改日期并且只是点击完成,我会得到 exc_bad_access 因为如果在没有将时间移动一分钟或更长时间的情况下点击完成,它似乎正在返回空。

我为 UIDatePicker 提取 NSDate 并将其传递给对其进行计算的方法,但是当刚刚完成时,它会崩溃。

有没有办法设置UIDatePicker的默认值,或者只是弹出的值? (通常是当前时间,但仍返回 null)。我一直在寻找一种方法来将默认值设置为午夜,或者让它简单地设置它弹出的当前时间。

这是我将其设置为午夜的尝试(没有用,崩溃了):

NSString *myDateAsAStringValue=@"2011-08-27 00:00:00 +0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString: myDateAsAStringValue];
[datePicker setDate:myDate];
[self changeDate:datePicker];

如果你不想通过链接,这里是来自 fluchpunkt 的代码:

- (void)changeDate:(UIDatePicker *)sender 
 NSLog(@"New Date: %@", sender.date);


- (void)removeViews:(id)object 
 [[self.view viewWithTag:9] removeFromSuperview];
 [[self.view viewWithTag:10] removeFromSuperview];
 [[self.view viewWithTag:11] removeFromSuperview];


- (void)dismissDatePicker:(id)sender 
 CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height, 320, 44);
 CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height+44, 320, 216);
 [UIView beginAnimations:@"MoveOut" context:nil];
 [self.view viewWithTag:9].alpha = 0;
 [self.view viewWithTag:10].frame = datePickerTargetFrame;
 [self.view viewWithTag:11].frame = toolbarTargetFrame;
 [UIView setAnimationDelegate:self];
 [UIView setAnimationDidStopSelector:@selector(removeViews:)];
 [UIView commitAnimations];


- (IBAction)callDP:(id)sender 
 if ([self.view viewWithTag:9]) 
  return;
 
 CGRect toolbarTargetFrame = CGRectMake(0, self.view.bounds.size.height-216-44, 320, 44);
 CGRect datePickerTargetFrame = CGRectMake(0, self.view.bounds.size.height-216, 320, 216);

 UIView *darkView = [[[UIView alloc] initWithFrame:self.view.bounds] autorelease];
 darkView.alpha = 0;
 darkView.backgroundColor = [UIColor blackColor];
 darkView.tag = 9;
 UITapGestureRecognizer *tapGesture = [[[UITapGestureRecognizer alloc]     initWithTarget:self         action:@selector(dismissDatePicker:)] autorelease];
 [darkView addGestureRecognizer:tapGesture];
 [self.view addSubview:darkView];

 UIDatePicker *datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height+44, 320, 216)] autorelease];
 datePicker.tag = 10;
 [datePicker addTarget:self action:@selector(changeDate:)     forControlEvents:UIControlEventValueChanged];
 [self.view addSubview:datePicker];

 UIToolbar *toolBar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)] autorelease];
 toolBar.tag = 11;
 toolBar.barStyle = UIBarStyleBlackTranslucent;
 UIBarButtonItem *spacer = [[[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
 UIBarButtonItem *doneButton = [[[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissDatePicker:)] autorelease];
 [toolBar setItems:[NSArray arrayWithObjects:spacer, doneButton, nil]];
 [self.view addSubview:toolBar];

 [UIView beginAnimations:@"MoveIn" context:nil];
 toolBar.frame = toolbarTargetFrame;
 datePicker.frame = datePickerTargetFrame;
 darkView.alpha = 0.5;
[UIView commitAnimations];

【问题讨论】:

【参考方案1】:

只需从 github 下载此代码即可。这是您需要的演示项目。

here

【讨论】:

以上是关于UIDatePicker 默认返回 Null?的主要内容,如果未能解决你的问题,请参考以下文章

使用UIAppearance自定义iOS UIDatepicker

具有 15m 间隔的 UIDatePicker 但始终将准确时间作为返回值

如何使 UIDatePicker 出现在屏幕底部?

iPad UI 的 JavaScript 模拟(特别是 UIDatePicker 和 UIPicker)

UIDatePicker:无法选择默认值

jquery ui datepicker怎么设置默认日期