gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:36:31 25 4
gpt4 key购买 nike

我在多个 UITextFields 上使用单个 UIDatePicker。当点击 UITextField 时,它会调用 UIDatePicker 并在选择器滚动时更新文本。这一切都很好,但现在我想做一些验证。我的 UITextFields 是“Employed From”和“Employed To”,我需要检查 “From” 字段是否始终小于 “To” 字段(即FROM 1995、TO 1990 期间不能雇用该人)。

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

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

我已经四处搜索,但似乎无法在为多个 UITextFields 重新使用单个 UIDatePicker 时找到有效的解决方案。这可能吗?或者我是否必须为每个字段创建一个新的UIDatePicker?非常感谢任何帮助!

最佳答案

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

关于ios - 使用单个 datePicker 验证多个 UITextField 上的日期(即 fromDate < toDate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26296221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com