Note that there are some explanatory texts on larger screens.

plurals
  1. POUIDatePicker with 2 Textfields (from/until date) can't set the right date at "until"textfield
    text
    copied!<p>i have one datepicker. i wanna let the user choose the date and time in "Von"textfield ( which starts at tomorrow at 9 am ) and then a until textfield "bis" ( which starts 1.5 hours later).</p> <p>everything works fine. but when i click on the "von" textfield. my datepicker dint set himself the correct time. </p> <p>Here are some screenshots: <a href="http://i44.tinypic.com/1omus3.jpg" rel="nofollow">from textfield</a>, <a href="http://i42.tinypic.com/i1y876.jpg" rel="nofollow">until textfield</a></p> <p>ViewDidLoad:</p> <pre><code>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; } </code></pre> <p>here is the action: </p> <pre><code>-(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; } } </code></pre> <p>here is where i set my datebool to no:</p> <pre><code>-(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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload