使用单个datePicker验证多个UITextField上的日期(即fromDate< toDate)

时间:2014-10-10 09:31:46

标签: ios objective-c validation uitextfield uidatepicker

我在多个UIDatePicker上使用了一个UITextFields。点击UITextField后,它会调用UIDatePicker并在滚动选择器时更新文本。这一切都很好,但现在我想做一些验证。我的UITextFields是"受雇于" &安培; "受雇于",我需要检查"From"字段是否始终小于"To"字段(即一个人不能被雇用FROM 1995 ,TO 1990)。

这是我的代码,它设置默认日期并为每个UITextField设置输入视图:

- (void)datePickerTextField {
    // Open date picker when textfield is tapped
    UIDatePicker *datePicker = [[UIDatePicker alloc]init];
    datePicker.datePickerMode = UIDatePickerModeDate;

    // Set default date
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc]init];
    [components setDay:1];
    [components setMonth:1];
    [components setYear:1980];
    NSDate *defaultDate = [calendar dateFromComponents:components];
    [datePicker setDate:defaultDate];

    [datePicker setMaximumDate:[NSDate date]];
    [datePicker addTarget:self
                   action:@selector(updateDateTextField:)
         forControlEvents:UIControlEventValueChanged];
    datePicker.backgroundColor = [UIColor clearColor];

    // Set input for each textfield
    [self.empFrom1 setInputView:datePicker];
    [self.empTo1 setInputView:datePicker];
    // ... more fields
    [self.empFrom10 setInputView:datePicker];
    [self.empTo10 setInputView:datePicker];
}

我尝试了以下内容:

- (void)updateDateTextField:(UIDatePicker *) datePicker {
    // Format date
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"dd/MM/yyyy"];
    NSString *stringFromDate = [formatter stringFromDate:datePicker.date];

    UIAlertView *validDate = [[UIAlertView alloc]initWithTitle:@"Error"
                                                       message:@"Please check dates"
                                                      delegate:nil
                                             cancelButtonTitle:@"Dismiss"
                                             otherButtonTitles:nil];

    // Check which textfield is active and set date
    if ([self.empFrom1 isFirstResponder]) {
        self.empFrom1.text = [NSString stringWithFormat:@"%@", stringFromDate];

        NSString *empFrom = self.empFrom1.text;
        NSString *lastFour = [empFrom substringFromIndex:empFrom.length - 4];
        _from = @([lastFour integerValue]);
        NSLog(@"FROM: %@", _from);
    }
    else if ([self.empTo1 isFirstResponder]) {
        self.empTo1.text = [NSString stringWithFormat:@"%@", stringFromDate];

        NSString *empTo = self.empTo1.text;
        NSString *lastFour = [empTo substringFromIndex:empTo.length - 4];
        _to = @([lastFour integerValue]);
        NSLog(@"TO: %@", _to);

        if (_from > _to) {
            [validDate show];
        }
    }

我不确定为什么上述代码段不起作用,因为NGLog明确输出了正确的integer,但即使FROM较少,警报仍会显示比TO

Attempted solution

我已经搜索过,在重复使用单UIDatePickerUITextFields时,似乎找不到可行的解决方案。这甚至可能吗?或者我是否必须为每个字段创建一个新的UIDatePicker?非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

- (void)updateDateTextField:(UIDatePicker *) datePicker { 

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; 
    [formatter setDateFormat:@"dd/MM/yyyy"]; 
    NSString *stringFromDate = [formatter stringFromDate:datePicker.date]; 

    UIAlertView *validDate = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please check dates" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 

    // Check which textfield is active and set date 
    if ([self.empFrom1 isFirstResponder]) {   
        self.empFrom1.text = [NSString stringWithFormat:@"%@", stringFromDate]; 
        self.dateFrom = datePicker.date; 
    } else if ([self.empTo1 isFirstResponder]) { 
        self.empTo1.text = [NSString stringWithFormat:@"%@", stringFromDate]; 
        self.dateTo = datePicker.date; 

        if( [self.dateFrom timeIntervalSinceDate:self.dateTo] > 0 ) { 
            [validDate show]; 
        } 
    } 
}

为您的班级添加2个NSDate属性,例如dateFrom& dateTo