2012-03-23 123 views
0

我有一個日期選擇器。我想讓用戶在「Von」文本字段(明天上午9點開始)中選擇日期和時間,然後直到textfield「bis」(1.5小時後開始)。UIDatePicker與2個文本框(從/直到日期)不能設置正確的日期在「直到」文本域

一切工作正常。但是當我點擊「馮」文本框。我的日期選擇器爲自己設定了正確的時間。

下面是一些截圖: from textfielduntil textfield

viewDidLoad中:

NSDate *today = [NSDate date]; 
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    [gregorian setLocale:[NSLocale currentLocale]]; 

    NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit|NSMonthCalendarUnit| NSWeekdayCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today]; 


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

    today = [gregorian dateFromComponents:nowComponents]; 

    NSDate *tomorrowat9am = [gregorian dateFromComponents:nowComponents]; 
    //DatePicker wird erstellt 

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



    //starts from tomorrow at 9 am 
    NSDate *pickereinstelldate= tomorrowat9am; 

    [self.thePicker setDate:pickereinstelldate]; 

    //set the right 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 *neuesdatum = [beginningOfWeek dateByAddingTimeInterval:3600*1.5];   NSString *neuesd = [dateFormatter stringFromDate:neuesdatum]; 


    //set the textfield with the new date 
    tfVon.text=datum; 
    tfBis.text=neuesd; 

    [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 "Von" textfield 
    if (dateBOOL==YES) 

    { 
     tfVon.text=datum; 

     tfBis.text=neuesd; 

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

     }  

} 

這裏是我我datebool設置爲NO:

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

} 
+1

不清楚的問題。請說清楚 – Hiren 2012-03-23 10:11:47

+0

您什麼時候顯示日期選擇器?你在哪裏設置'dateBOOL'爲'NO'?你可以發佈一些代碼嗎? – sch 2012-03-23 10:34:16

+0

當我選擇textxfield(我更新了我的代碼)時,顯示我的日期選擇器。在textfield中的記事本應該被限制爲 – 2012-03-23 12:52:10

回答

0

您發佈的最後一個方法永遠不會讓它最後返回YES。你有一個額外的}。

「我的日期選擇器爲自己設定了正確的時間。」 您的問題並不完全清楚,但從我收集的內容來看,您正嘗試在選擇「bis」文本框後將日期選擇器設置爲正確的時間。

[datePicker setDate:neuesdatum animated:NO];

如果您嘗試將bis文本框設置爲新日期,則必須再次使用dateformatter並將文本框設置爲該新日期。

以下哪一行(neuesd)給你當前的值? NSString * neuesd = [dateFormatter stringFromDate:neuesdatum];

+0

我刪除了一些不重要的代碼,並且我忘記刪除}符號。 nsstring * neuesd從datepicker獲取值並將其設置爲1.5小時後。當我點擊「bis」文本框時,我想將datepicker日期設置爲neuesd。所以我可以在1.5小時後開始 – 2012-03-24 09:20:28