0

I was trying to set up a uidatepicker for booking some events in the calendar. I need to disable (hide) some particular dates in my UIDatePicker, for instance days when I'm out of town or on vacation.

Can you help me with this one? Thanx a lot in advance. Here is some code:

 - (void)viewDidLoad
 {


     NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"IT"];
     [self.datePicker setLocale:locale];

     NSString *min = @"20132015";
     NSString *max = @"22132015";

     NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
     [dateFormat setDateFormat:@"ddMMyyyy HH:mm"];
     NSDate *datemin = [dateFormat dateFromString:min];
     NSDate *datemax = [dateFormat dateFromString:max];
[self.view addSubview:datePicker];
datePicker.minuteInterval = 15;
datePicker.maximumDate = datemax;
datePicker.minimumDate = [NSDate date];

     [datePicker addTarget:self action:@selector(disableDate)      forControlEvents:UIControlEventValueChanged];

[datePicker addTarget:self
                action:@selector(LabelChange:)
      forControlEvents:UIControlEventValueChanged];


[super viewDidLoad];
// Do any additional setup after loading the view.
 }

 -(void)disableDate{

NSDate *pickedDate = datePicker.date;  // Get current Date
NSDate *disabledDate = [?????;
if([pickedDate compare:disabledDate] == NSOrderedSame)
{
    [datePicker setDate:disabledDate animated:YES];

}
 }
      -(void)getDisableDate
 {
?????
 }


 - (void)LabelChange:(id)sender{


     NSDateFormatter *df = [[NSDateFormatter alloc] init];
     [df setDateFormat:@"dd.MM.yyyy HH:mm"];
     NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"IT"];
     [self.datePicker setLocale:locale];

     labelDate.text = [NSString stringWithFormat:@"%@",
                    [df stringFromDate:datePicker.date]];

     NSLog(@"date:%@", datePicker.date);


 }

2 Answers2

0

UIDatePicker doesn't support ranges of dates. What I would do instead is to change the date whenever a bad date is selected, by using setDate:animated: to the nearest date that is selectable. Also, to show a message somewhere explaining why the selected date was changed.

Either that, or, write your own custom UIDatePicker using UIPickerView. Not too hard, I've done it before. The worst part is dealing with wrapping values (eg: 31 -> 1). Does involve more work, though.

zadr
  • 2,505
  • 18
  • 19
0

Best solution is create custom UIDatePicker using UIPickerView it's too easy and in future you can do changes easily.

Check the link.

Community
  • 1
  • 1
Chirag Pipaliya
  • 1,281
  • 12
  • 20