0

大家好我有问题

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

我怎样才能在这些日期“阻止选择器”?

这是我的代码,希望对您有所帮助

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

1 回答 1

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

澄清后编辑

如果您想在应用程序启动之间使用日期,则需要以某种方式保留该日期。我建议使用NSUserDefaults,但您也可以使用更复杂的 Core Data 解决方案。使用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];
}

那么当用户改变picker的值时,就需要存储新的值。

// When the datePicker value has changed, we store the value
[[NSUserDefaults standardUserDefaults] setObject:alarmDatePickerUno.date forKey:@"PickerUnoKey"];
于 2012-04-05T21:21:07.203 回答