Reputation: 643
I am trying to set maximum and minimum values to my Date Picker But it does not set this properties correctly.
I did this like below:
- (void)viewDidLoad {
[super viewDidLoad];
[self setLimitForDatePicker];
birthdayTextField.inputView = _dateWithYearPicker;
}
-(void)awakeFromNib
{
_dateWithYearPicker = [[UIDatePicker alloc] init];
_dateWithYearPicker.datePickerMode = UIDatePickerModeDate;
[_dateWithYearPicker addTarget:self action:@selector(datePickerDateDidChange) forControlEvents:UIControlEventValueChanged];
}
-(void)setLimitForDatePicker
{
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-16];
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-110];
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
_dateWithYearPicker.minimumDate = minDate;
_dateWithYearPicker.maximumDate = maxDate;
}
After that when I launch application, and click on the birthdayTextField, the date picker appears to have not limits.
All your suggestions are welcome.
Upvotes: 0
Views: 740
Reputation: 643
I found the problem.
I switched the min and max date, the correct answer is :
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:-110]; //before it was 16
NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
[comps setYear:-16]; // before it was 110
NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0];
_dateWithYearPicker.minimumDate = minDate;
_dateWithYearPicker.maximumDate = maxDate;
Upvotes: 1