1

我有一个日期选择器。我想让用户在“from”文本字段(明天上午 9 点开始)中选择日期和时间,然后选择文本字段“直到”(1.5 小时后开始)。

从 TextFiled 到 TextField

例如。“从”文本字段:25.04.2012 09:00,“直到”文本字段:25.04.2012 10:30

看一下我上传的图片。

到目前为止一切正常,但如果我点击“直到”文本字段,日期选择器没有设置正确的日期和时间。他设置了 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 设置为 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;

    }

有没有办法可以将“直到”文本字段中的 DatePicker 设置为正确的时间?

4

1 回答 1

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;
}
于 2012-04-24T09:21:22.560 回答