繁体   English   中英

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

[英]UIDatePicker with 2 Textfields (from/until date)?

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

从TextFiled 直到TextField

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

看一下我上传的图片。

到目前为止一切正常,但是如果我单击“直到”文本字段,则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设置为正确的时间?

如果我得到您的帮助,您希望日期选择器显示所选文本字段用户的日期。

由于您使用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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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