简体   繁体   English

UIDatePicker默认返回Null吗?

[英]UIDatePicker Returns Null By Default?

My implementation for UIDatePicker is essentially this one . 我对UIDatePicker的实现本质上就是这个

My problem is that if the UIDatePicker pops up and the user doesn't change the date and just hits done, I get exc_bad_access because if done is hit without having moved the time by a minute or more, it seems to be returning null. 我的问题是,如果UIDatePicker弹出并且用户不更改日期而只是点击完成,我会得到exc_bad_access,因为如果点击完成而没有将时间移动一分钟或更长时间,它似乎将返回null。

I pull the NSDate for the UIDatePicker and pass it to a method that does a calculation on it, however when done is just hit, it crashes. 我为UIDatePicker拉NSDate并将其传递给在其上进行计算的方法,但是当刚完成时,它会崩溃。

Is there any way to set the default value of the UIDatePicker, or just have it be the value that pops up? 有什么方法可以设置UIDatePicker的默认值,还是让它成为弹出的值? (Which is typically the current time, but still returns null). (通常是当前时间,但仍返回null)。 I've been searching for a way to either set the default value to midnight, or to have it simply set the current time that it pops up as. 我一直在寻找一种将默认值设置为午夜的方法,或者只是将其设置为弹出的当前时间。

Here's my attempt at setting it to midnight (didn't work, crashed): 这是我尝试将其设置为午夜(无效,崩溃):

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];

Here's the code from fluchpunkt if you don't wanna bother going through the link: 如果您不想打扰链接,这是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];
}

Just go and download this code from github. 只需从github下载此代码即可。 It is what u need with a demo project inside. 这是您需要的一个内部演示项目。

here 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM