简体   繁体   中英

UIDatePicker with 2 Textfields (from/until date)?

I have a DatePicker. I wanna let the User choose the date and time in "from"textfield (which starts at tomorrow at 9 am) and then a textfield "until" ( which starts 1.5 hours later).

From TextFiled Until TextField

For Example. "From" TextField: 25.04.2012 09:00, "Until" TextField: 25.04.2012 10:30

Take a look at the picture, which i have uploaded.

Everything works fine so far, but if i click on the "until" textfield the Datepicker did not set the correct Date and Time. He Set 25.04.2012 09:00 instead of 25.04.2012 10:30

My Code so far:

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;


}

The Action function:

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

        }    

}

here is where i set my datebool to 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;

    }

Is There a way that i can set the DatePicker in "Until" TextField to the correct Time?

If I got you, you want the date picker to show the date of the text field user selected.

Since you work with textFieldShouldBeginEditing , I post code within this method too (but don't you think textFieldDidBeginEditing would be a better choice).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM