2012-04-24 66 views
1

我有一個DatePicker。我想讓用戶在「from」文本框(明天上午9點開始)中選擇日期和時間,然後在「直到」(1.5小時後開始)顯示文本框。帶2個Textfields的UIDatePicker(從/到日期)?

From TextFiledUntil TextField

對於實施例。 「From」TextField:25.04.2012 09:00,「Until」TextField:25.04.2012 10:30

看看我上傳的圖片。

到目前爲止一切正常,但如果我點擊「直到」文本字段Datepicker沒有設置正確的日期和時間。他成立25.04.2012 09:00,而不是25.04.2012 10:30

到目前爲止我的代碼:

ViewDidLoad: 

    //Create DatePicker 

    self.thePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 152, 325, 300)]; 
    self.thePicker.datePickerMode = UIDatePickerModeDateAndTime; 
    [thePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged]; 


    //Set DatePicker to Tomorrow 9:00 o'clock 

    NSDate *today = [NSDate date]; 

    NSCalendar *german = [[NSCalendar alloc]   initWithCalendarIdentifier:NSGregorianCalendar]; 

    [german setLocale:[NSLocale currentLocale]]; 
    NSDateComponents *nowComponents = [german components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSDayCalendarUnit| NSMonthCalendarUnit| NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today]; 

    [nowComponents setDay:[nowComponents day] +1]; 
    [nowComponents setWeek: [nowComponents week]]; 
    [nowComponents setHour:9]; 
    [nowComponents setMinute:0]; 
    [nowComponents setSecond:0]; 

    NSDate *beginningOfWeek = [german dateFromComponents:nowComponents]; 
    NSDate *pickereinstelldate= beginningOfWeek; 

    [self.thePicker setDate:pickereinstelldate]; 


    //Set DatePicker to correct Format 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    NSString *datum =[dateFormatter stringFromDate:self.thePicker.date]; 

    //set the date at textfield "bis" to 1.5 hours later 
    NSDate *morgenDate = [beginningOfWeek dateByAddingTimeInterval:3600*1.5]; 
    NSString *newdate = [dateFormatter stringFromDate:morgenDate]; 

    //Set the until TextField with the newdate 

    tfVon.text=datum; 
    tfBis.text=newdate; 


    [self.view addSubview:thePicker]; 
    self.thePicker.hidden=YES; 
    tfVon.inputView=thePicker; 
    tfBis.inputView=thePicker; 


} 

行動功能:

-(IBAction)dateChanged 
    { 
    self.date = [thePicker date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    NSString *datum =[dateFormatter stringFromDate:self.date]; 
    NSDate *neuesdatum = [date dateByAddingTimeInterval:3600*1.5]; 
    NSString *neuesd = [dateFormatter stringFromDate:neuesdatum]; 

    //when i click on "From" textfield 
    if (dateBOOL==YES) 

    { 
     tfVon.text=datum; 

     tfBis.text=neuesd; 

    } 
     //when i click on "Until" textfield 
     if (dateBOOL==NO) { 
     tfBis.text=neuesd; 

     }  

} 

這裏是我把我的datebool無:

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
    { 


    if (textField==tfVon) 
    { 

     dateBOOL=YES; 
     self.thePicker.hidden=NO; 

     return YES; 
    } 
    if (textField==tfBis) 
    { 

     dateBOOL=NO; 
     self.thePicker.hidden=NO; 
     return YES; 
    } 

    } 
    return YES; 

    } 

是沒有辦法,我可以在「直到」文本字段設置的DatePicker爲正確的時間的方法嗎?

回答

1

如果我找到了你,你希望日期選擇器顯示用戶選擇的文本字段的日期。

既然您使用textFieldShouldBeginEditing,我也會在此方法中發佈代碼(但您不認爲textFieldDidBeginEditing會是更好的選擇)。

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField==tfVon) 
    { 
     dateBOOL=YES; 
    } 
    else if (textField==tfBis) 
    { 
     dateBOOL=NO; 
    } 
    else 
    { 
     //?? should not happen 
    } 
    self.thePicker.hidden=NO; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"dd.MM.yyyy HH:mm"]; 
    NSDate *dateOfCurrentTextField = [dateFormatter dateFromString:textField.text]; 
    [self.picker setDate: dateOfCurrentTextField animated:YES]; 
    return YES; 
} 
+0

首先感謝您的幫助。它完美的作品。嗯,我只是用textFieldShouldBeginnEditing試過。我如何使用textfieldDidBeginnEditing,因爲這種方法的類型是無效的。 – Bashud 2012-04-24 09:43:19

+0

textFieldDidBeginEditing是無效的,因爲它在用戶開始編輯時被調用,這意味着某事告訴文本字段它允許。這可以通過textFieldShouldBeginEditing實現(就像你做的那樣)或默認的(如果你沒有實現該方法)來完成。 Ur impl。 textfieldDidBeginnEditing應該看起來像你的impl。 textFieldShouldBeginnEditing除了不返回任何內容。 textFieldShouldBeginnEditing適用於在運行時決定的程序(取決於任何情況)是否允許用戶編輯,即它們有時會返回YES,有時會返回NO。 – 2012-04-24 10:01:33

+0

哦,好的謝謝你的解釋。現在我認爲textFieldDidBeginnEditing是更好的選擇!再次,thx爲您的幫助 – Bashud 2012-04-24 10:13:01