UIDatePicker有2个文本字段(从/到日期)?

时间:2012-04-24 08:42:23

标签: ios xcode datepicker

我有一个DatePicker。我想让用户选择"来自" textfield(明天上午9点开始)然后是文本字段"直到" (1.5小时后开始)。

From TextFiled Until TextField

例如。 "从" TextField:25.04.2012 09:00,"直到" TextField:25.04.2012 10:30

看一下我上传的图片。

到目前为止一切正常,但如果我点击"直到" textfield 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设置为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? TextField到正确的时间?

1 个答案:

答案 0 :(得分: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;
}