我有 uilocalnotification ,我使用 NSDateComponents 为每个通知设置不同的时间,具体取决于项目应该得到通知的次数。
-(NSArray *)fireDatesForFrequency:(int)freq
{
if (freq == 1)
{
[components setHour:8];
[components setMinute:00];
NSDate *date = [gregorian dateFromComponents:components];
NSArray *dates =[[NSArray alloc]initWithObjects:date, nil];
return dates;
}
if (freq == 2) {
[components setHour:8];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:20];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2, nil];
return dates;
}
if (freq == 3)
{
[components setHour:8];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:12];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[components setHour:16];
[components setMinute:00];
NSDate *date3 = [gregorian dateFromComponents:components];
NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3, nil];
return dates;
}
if (freq == 4)
{
[components setHour:8];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:12];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
[components setHour:16];
[components setMinute:00];
NSDate *date3 = [gregorian dateFromComponents:components];
[components setHour:20];
[components setMinute:00];
NSDate *date4 = [gregorian dateFromComponents:components];
NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3,date4, nil];
return dates;
}
return nil;
}
现在我想将这些时间存储在 NSUserDefaults 中,这样如果用户想根据他的需要更改时间,我想将这些时间存储在 NSUserdefaults 中。我想用 userupdatednsuserdefaults 替换默认的 NSuserdefaults ..datepicker 有分段控制,所以当用户从 datepicker 选择时间时,时间应该更新为默认的 nsuserdefaults 。所以如果用户选择第一个分段控制项,则该值应该存储在
- (IBAction)didChangeSegmentControl:(UISegmentedControl *)control
{
NSInteger index = [notificationSessionTab selectedSegmentIndex];
if (index == 0)
{
[userTimePicker reloadInputViews];
[components setHour:7];;
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:9];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
//NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3,date4, nil];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
NSDate *selectedTime =[userTimePicker date];
}
if (index == 1)
{
[userTimePicker reloadInputViews];
[components setHour:11];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:13];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
//NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3,date4, nil];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
NSDate *selectedTime =[userTimePicker date];
}
if (index == 2)
{
[userTimePicker reloadInputViews];
[components setHour:15];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:18];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
//NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3,date4, nil];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
NSDate *selectedTime =[userTimePicker date];
}
if (index == 3)
{
[userTimePicker reloadInputViews];
[components setHour:19];
[components setMinute:00];
NSDate *date1 = [gregorian dateFromComponents:components];
[components setHour:21];
[components setMinute:00];
NSDate *date2 = [gregorian dateFromComponents:components];
//NSArray *dates =[[NSArray alloc]initWithObjects:date1,date2,date3,date4, nil];
[userTimePicker setDatePickerMode:UIDatePickerModeTime];
[userTimePicker setMinimumDate:date1];
[userTimePicker setMaximumDate:date2];
NSDate *selectedTime =[userTimePicker date];
}
}