在特定时间设置datepicker

时间:2012-04-05 21:14:19

标签: iphone datepicker alarm

大家好我有问题

我的代码我设置了一个日期选择器,其中包含今天的日期,之后如果用户更改了选择器,我会设置一个警报,以便在选择器日期响铃。

如何在这些日期“阻止选择器”?

这里是我的代码,我希望在你的帮助下

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);

1 个答案:

答案 0 :(得分:1)

alarmDatePickerUno.userInteractionEnabled = NO;
alarmDatePickerDue.userInteractionEnabled = NO;

澄清后编辑

如果您想在应用启动之间使用日期,则需要以某种方式保留日期。我建议使用NSUserDefaults,但你也可以选择更复杂的核心数据解决方案。使用NSUserDefaults,每次为保存的值创建UIDatePicker时都会检查,并使用它来初始化选择器。

// 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];
}

然后,当用户更改选择器的值时,您需要存储新值。

// When the datePicker value has changed, we store the value
[[NSUserDefaults standardUserDefaults] setObject:alarmDatePickerUno.date forKey:@"PickerUnoKey"];