Search code examples
objective-cuidatepicker

Restrict the UIDatePicker control based on type, say (Adult, Child, Infant) Objective C


Am trying to restrict the UIDatePicker control based on type, say (Adult, Child, Infant). Below is what am currently doing to call the DatePicker

cell.leftDetailTextField.inputView=[self customDatePickerView];

-(UIDatePicker *)customDatePickerView{
    NSLog (@"Date picker called");
    datePicker = [[UIDatePicker alloc]init];
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    //datePicker.maximumDate=[NSDate date];  // to prevent future date selection:
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDate *currentDate = [NSDate date];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setYear:-90];
    NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
    [comps setYear:-12];
    NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
    
    datePicker.minimumDate = minDate;
    datePicker.maximumDate = maxDate;
    
    [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    return datePicker;
}

How can I dynamically pass a value to this method call. I need to pass Adult(val 1), Children(Val 2), Infant (val 3)


Solution

  • -(UIDatePicker *)customDatePickerView:(NSInteger)val{
        datePicker = [[UIDatePicker alloc]init];
        [datePicker setDatePickerMode:UIDatePickerModeDate];
        
        //datePicker.maximumDate=[NSDate date];  // to prevent future date selection:
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        NSDate *currentDate = [NSDate date];
        NSDateComponents *comps = [[NSDateComponents alloc] init];
        NSDate *minDate, *maxDate;
        switch (val) {
            case 1: // Adult
                    [comps setYear:-90];
                    minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                    [comps setYear:-12];
                    maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                break;
            case 2: // Child
                    [comps setYear:-12];
                    minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                    [comps setYear:-2];
                    maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                break;
            case 3: // Infant
                    [comps setYear:-2];
                    minDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                    [comps setYear:-0];
                    maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate  options:0];
                break;
                
            default:
                break;
        }
        
        datePicker.minimumDate = minDate;
        datePicker.maximumDate = maxDate;
        
        [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        return datePicker;
    }
    
    - (void) dateChanged:(UIDatePicker *)sender{
        UIDatePicker *datePicker=(UIDatePicker *)sender;
        NSDateFormatter *dateFormat =[[NSDateFormatter alloc]init];
        [dateFormat setDateFormat:@"dd-MMM-yyyy"];
        self.activeTextField.text = [dateFormat stringFromDate:datePicker.date];
    }