【问题标题】:How to set a reminder before two weeks of the selected date in ios如何在ios中选择日期的两周前设置提醒
【发布时间】:2016-03-29 06:57:32
【问题描述】:

我有以下要求,

  1. 首先需要选择日期。
  2. 然后应用需要显示提醒选项,例如(2 周前、1 周前、3 天前等)
  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;
        }
}

【问题讨论】:

    标签: ios nsdate nscalendar


    【解决方案1】:
    1. 获取两周前的日期

      NSDate *selecteDate = YOUR_DATE; NSDate *twoWeeksAgoDate = [selecteDate dateByAddingTimeInterval:-14*24*60*60];// -14 是 14 天(2 周)之前。您需要根据自己的意愿进行更改。 NSLog(@"两周前:%@", twoWeeksAgoDate);

    2. 为 twoWeeksAgo Date 设置本地通知

      UILocalNotification *notification = [[UILocalNotification alloc]init]; notification.repeatInterval = NSDayCalendarUnit; [通知 setAlertBody:@"Your Reminder"]; [通知 setFireDate:twoWeeksAgoDate]; [通知 setTimeZone:[NSTimeZone defaultTimeZone]]; [应用setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-06-07
      • 1970-01-01
      • 2021-11-05
      相关资源
      最近更新 更多