Hi guys i have a problem
i my code i set a datepicker with the date of today, after that if the user change the picker i set a alarm to ring at the date of picker.
How can i "block the picker" on these date?
here my code, i hope in your help
NSDate *pickerDate = [NSDate date];
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
// Divisione della data in fattori
NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit )
fromDate:pickerDate];
NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
fromDate:pickerDate];
// Set Up dell'alarme considerando tutti i fattori
NSDateComponents *dateComps = [[NSDateComponents alloc] init];
[dateComps setDay:[dateComponents day]];
[dateComps setMonth:[dateComponents month]];
[dateComps setYear:[dateComponents year]];
[dateComps setHour:[timeComponents hour]];
[dateComps setMinute:[timeComponents minute]];
//Notifica in meno di un minuto
[dateComps setSecond:0];
NSDate *itemDate = [calendar dateFromComponents:dateComps];
[dateComps release];
[alarmDatePickerUno setDate:itemDate animated:YES];
[alarmDatePickerDue setDate:itemDate animated:YES];
}
// ALARM UNO
// Set Allarme Uno
-(IBAction)setAlarmUno:(id)sender{
NSLog(@"picker in allarme");
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
//Setting dell'allarme in base al picker
AlarmDateUno = [alarmDatePickerUno date];
NSLog(@"allarme settata alle");
NSLog(@"%@", AlarmDateUno);
alarmDatePickerUno.userInteractionEnabled = NO;
alarmDatePickerDue.userInteractionEnabled = NO;
EDIT AFTER CLARIFICATION
If you want to use a date between app launches, you need to persist the date somehow. I would suggest using NSUserDefaults
, but you could also go for the more complicated solution of Core Data. Using NSUserDefaults
you would check every time you create your UIDatePicker
for the saved value and use that to initialize the picker.
// Get the date
NSDate *unoDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"PickerUnoKey"];
if (unoDate) { // If we have a persisted date, we set the date of the datePicker
[alarmDatePickerUno setDate:unoDate animated:NO];
}
Then when the user changes the value of the picker, you need to store the new value.
// When the datePicker value has changed, we store the value
[[NSUserDefaults standardUserDefaults] setObject:alarmDatePickerUno.date forKey:@"PickerUnoKey"];