2013-05-17 21 views
0

我想通過一個選定的時間與UIDatePicker來更改標籤的文本,但是當我更新它時,它總是以0:01結束。UIDatePickerView日期始終是加載0:01

我已經嘗試在新項目中使用自己的代碼,它的功能,所以這是關於我的其他功能。

- (void)viewDidLoad {  
NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"HH:mm"]; 
pickerLabel.text = [NSString stringWithFormat:@"%@", 
       [df stringFromDate:[NSDate date]]]; 
[self.view addSubview:pickerLabel]; 
[self.view addSubview:picker];  
} 

當完成按鈕被點擊時,它會更新標籤值並將datepicker的uiview動畫出來。

- (IBAction)savePalette:(id)sender { 

[UIView beginAnimations:@"animateView" context:nil]; 
[UIView setAnimationDuration:0.5]; 

CGRect viewFrame = [animatedView frame]; 
viewFrame.origin.y = 480; 
animatedView.frame = viewFrame; 

animatedView.alpha = 1.0; 
[self.view addSubview:animatedView]; 
[UIView commitAnimations]; 
NSLog(@"saving and close"); 

NSDateFormatter *df = [[NSDateFormatter alloc] init]; 
[df setDateFormat:@"HH:mm"]; 
pickerLabel.text = [NSString stringWithFormat:@"%@", [df stringFromDate:picker.date]]; 
NSLog([NSString stringWithFormat:@"%@",[df stringFromDate:picker.date]]); 
} 

任何幫助表示讚賞,謝謝!我認爲問題在於從選取器中取出的日期不會被轉移。

謝謝!

回答

0

你是如何從datepicker中選擇的日期? i。你必須指定你是如何做到這一點的。你說的是一個完成的按鈕 - 是否用於選擇日期顯示在選取器上而無需滾動選取器並選擇日期?

我遇到了類似的問題,像你有,並得到了固定餵養日期選擇器。

datePicker= [[UIDatePicker alloc] initWithFrame:PICKER_FRAME; 
[datePicker addTarget:self action:@selector(selectedTime:) forControlEvents:UIControlEventValueChanged]; 

這裏datePicker加載的默認日期是[NSdate date]。 現在試試這個;

NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"HH:mm"]; 
    //Setting the locale causes dateformatter to format string in 12 hr format. 
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
[dateFormatter setLocale:locale]; 
NSString *dateString =[dateFormatter stringFromDate:date]; 
NSDate *pickerLoadingDate = [dateFormatter dateFromString:dateString]; 
datePicker.date = pickerLoadingDate; 
+0

我想我不知道如何從完成按鈕中選擇日期。理想情況下,完成按鈕關閉調色板並從日期選取器中提取日期。你的第二個代碼是selectedTime:方法嗎? –

+0

沒有第二位代碼純粹是爲了datepicker初始化。方法selectedTime用於選擇日期選擇器日期。每次datePicker收到一個滾動並且在一個日期中靜止時,都會調用此方法。因此,使用完成按鈕,您必須強制調用selectedTime方法(在選定的時間方法內,您可以通過datePicker.date獲取日期)。 – Xcoder

+0

UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@「Done」 style:UIBarButtonItemStyleBordered target:self action:@selector(selectedTime :)]; – Xcoder

相關問題