我下面有一个要求,
- 首先需要选择日期
- 然后应用程序需要显示提醒选项,如(2周前,1周前,3天前等)
- 如果选择的日期是两天后,那么我需要禁用2周前和1周前选项
以下是我使用但不起作用的代码,
- (IBAction)timePicker:(id)sender {
selectedTimeRow = [sender tag];
NSDate *date2=[NSDate date];
NSDate *DaysAgo;
if (selectedTimeRow==0) {
DaysAgo = [date2 dateByAddingTimeInterval:14*24*60*60];
NSLog(@"14 days ago: %@", DaysAgo);
}else if (selectedTimeRow==1){
DaysAgo = [date2 dateByAddingTimeInterval:7*24*60*60];
NSLog(@"7 days ago: %@", DaysAgo);
}else if (selectedTimeRow==2){
DaysAgo = [date2 dateByAddingTimeInterval:3*24*60*60];
NSLog(@"3 days ago: %@", DaysAgo);
}else if (selectedTimeRow==3){
DaysAgo = [date2 dateByAddingTimeInterval:1*24*60*60];
NSLog(@"1 days ago: %@", DaysAgo);
}
NSDateFormatter *format = [[NSDateFormatter alloc] init];
format.dateFormat = @"dd/MM/yyyy";
NSString *dateNew=[format stringFromDate:DaysAgo];
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell*)[button superview];
//
NSComparisonResult result = [dateNew compare:_SavedDate];
if(result == NSOrderedAscending)
{
NSLog(@"date1 is later than date2");
cell.userInteractionEnabled=NO;
}else if (result == NSOrderedDescending) {
NSLog(@"date1 is earlier than date2");
[self calltoolrBar];
cell.userInteractionEnabled=YES;
}
else
{
NSLog(@"date1 is equal to date2");
[self calltoolrBar];
cell.userInteractionEnabled=YES;
}
}
-
获取两周前的日期
NSDate*selectedDate=YOUR_DATE;NSDate*twoWeeksAgoDate=[selectedDateByAddingTimeInterval:-14*24*60*60];//-14是14天前(2周)。你需要按照自己的意愿改变。NSLog(@"两周前:%@",twoWeeksAgoDate);
-
设置两周前日期的本地通知
UILocalNotification*通知=[[UILocalNotification alloc]init];notification.repeatInterval=NSDayCalendarUnit;[通知setAlertBody:@"您的提醒"];[通知setFireDate:towWeeksAgoDate];[通知setTimeZone:[NSTimeZone defaultTimeZone]];[application setScheduledLocalNotifications:[NArray arrayWithObject:notification]];