Showing datepicker 18 years ago and block ui datepicker in ios

In my project, I need to show the date picker 18, and I need to block 80 years ago, since I can show this date in datepicker, can someone help me find here here I am adding the code, my code that I printed in the journal but i need to display on my datepicker since i can display

NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; self.datePickerView.minimumDate = minDate; self.datePickerView.maximumDate = maxDate; NSLog(@"minDate %@", minDate); NSLog(@"maxDate%@", maxDate); 
+6
source share
2 answers

Hi, first of all, the minimum date should be 100 years ago, and the maximum date should be 18 year date, and then set the pickerView date to a date between the date range, so that your final code looks like

 NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; self.datePickerView.minimumDate = minDate; self.datePickerView.maximumDate = maxDate; self.datePickerView.date = minDate; 

Hope this will be helpful for you.

+21
source

Here is the viewDidLoad method of the view controller class

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSCalendar * gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDate * currentDate = [NSDate date]; NSDateComponents * comps = [[NSDateComponents alloc] init]; [comps setYear: -18]; NSDate * maxDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps setYear: -100]; NSDate * minDate = [gregorian dateByAddingComponents: comps toDate: currentDate options: 0]; [comps release]; UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 300)]; [pickerView setMinimumDate:minDate]; [pickerView setMaximumDate:maxDate]; [pickerView setDate:minDate]; [[self view] addSubview:pickerView]; } 

My conclusion

enter image description here

can you share your output as a screenshot

+2
source

Source: https://habr.com/ru/post/927535/


All Articles