2017-01-09 22 views
0

我有一個自定義視圖,在頂部附加了一個UIDatePicker和一個完成按鈕。UIDatePicker在「完成」按鈕上獲取.date屬性按

當我按下完成按鈕,我想要得到的時間是在UIDatePicker

我收到一個錯誤,當我按下按鈕(這是我期待,因爲發件人是的UIBarButtonItem)和更改日期時間按預期工作。

其他解決方案顯示了獨立的選擇,但是,我想(如果可能的話)來調用相同的選擇和獲得所選擇的,就好像它是在選擇器視圖滾動。

我想改變選擇的方法來接受它作爲一個id但是,沒有工作的。

這是我在做什麼目前,我覺得這可能是一個愚蠢的:

UIView *v = [[UIView alloc] init]; 

// Create the date time picker 
CGRect pickerFrame = CGRectMake(0, 0, 320, 216); 
UIDatePicker *pView = [[UIDatePicker alloc] initWithFrame:pickerFrame]; 

pView.datePickerMode = UIDatePickerModeTime; 
[pView addTarget:self action:@selector(dateTimePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 

dateTimePicker = pView; 
[v addSubview:dateTimePicker]; 

// done button 
CGRect toolBarFrame = CGRectMake(0, 0, 320, 44); 
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:toolBarFrame]; 
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil]; 
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(dateTimePickerValueChanged:)]; 
NSArray *toolBarItems = [NSArray arrayWithObjects:flexibleItem, doneButton, nil]; 
[toolBar setItems:toolBarItems]; 
[v addSubview:toolBar]; 

- (void)dateTimePickerValueChanged:(UIDatePicker *)datePicker { 
    NSLog(@"time on change: %@", datePicker.date); 

圖片參考:

enter image description here

回答

1

寫你的方法不帶參數,使您UIDatePicker作爲屬性或全局對象爲整個類,這樣就可以從任何地方訪問它。

現在將您的方法傳遞給兩個選擇器,這樣您將在完成按鈕按下時實現日期。

- (void)dateTimePickerValueChanged { 
    NSLog(@"time on change: %@", datePicker.date); 
} 
1

你說得對,這是什麼造成崩潰。我認爲分隔選擇器是正確的道路。您可以讓兩個選擇器調用相同的方法。這樣你就沒有任何複製的代碼。

+0

我怎麼會得到那就是在選擇器視圖,然後如果我的選擇單獨的時間?我的問題是試圖找出如何獲取日期實例進入完成選擇器。除非我不能那樣做? – Simon

+0

使日期選擇器成爲該類的一個屬性。你可以這樣訪問它。 –

0

要做到這一點的正確方法是創建一個NSDate對象,該對象可根據您的要求進行訪問。現在從UIDatePicker,創建一個值更改的方法。每當此方法被調用時,將日期選擇器.date屬性的值設置爲您的NSDate對象。 按完成按鈕,使用NSDate對象的值。它會給你正確的價值。

@interface ViewController(){ 
NSDate *globalDate; 
} 
@end 

@implementation ViewController 

- (void)dateTimePickerValueChanged { 
    globalDate = datePicker.date; 
} 

- (IBAction)donePressed { 
NSLog(@"%@",globalDate); 
} 

@end 
-1

TIME

datePicker=[[UIDatePicker alloc]init]; 
datePicker.datePickerMode=UIDatePickerModeTime; 
[yourField setInputView:datePicker]; 
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)]; 
[toolBar setTintColor:[UIColor grayColor]]; 
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStylePlain target:self action:@selector(ShowSelectedDate)]; 


UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 



[toolBar setItems:[NSArray arrayWithObjects:space,doneBtn, nil]]; 
[yourField setInputAccessoryView:toolBar]; 



-(void)ShowSelectedDate 
{ 
NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init]; 
dateFormat.dateStyle=NSDateFormatterMediumStyle; 
[dateFormat setDateFormat:@"hh:mm"]; 
NSString *str=[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:datePicker.date]]; 
//assign text to label 
yourField =str; 
[yourField resignFirstResponder]; 

[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDelegate:self]; 
[UIView setAnimationDuration:0.5]; 
[UIView setAnimationBeginsFromCurrentState:YES]; 
self.view.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height); 
[UIView commitAnimations]; 
} 
+0

您應該使用UIView動畫塊來代替舊的傳統動畫風格。 –