2011-08-27 184 views
0

裸陪我在這裏,iPhone編程小白試圖弄清楚這一點...UIDatePicker默認返回空值?

我對實施的UIDatePicker基本上是這裏的一個:

UIDatePicker pop up after UIButton is pressed

我的問題是,如果在的UIDatePicker彈出並且用戶沒有更改日期並且只是完成了,我得到exc_bad_access,因爲如果完成時沒有將時間移動一分鐘或更長時間,它似乎返回null。

我爲UIDatePicker提取NSDate並將其傳遞給對其執行計算的方法,但是當完成時只是命中,它會崩潰。

有什麼方法可以設置UIDatePicker的默認值,或者只是讓它彈出的值? (通常是當前時間,但仍然返回null)。我一直在尋找一種方法來將默認值設置爲午夜,或者只是設置它彈出的當前時間。

這是我嘗試在其設置爲午夜(沒有工作,墜毀):

NSString *[email protected]"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]; 
} 

謝謝!

回答

0

只需從github下載此代碼。這是你需要一個演示項目。

here