如何在ios中所选日期的两周之前设置提醒

时间:2016-03-29 06:57:33

标签: ios nsdate nscalendar

我有以下要求,

  1. 首先需要选择日期。
  2. 然后应用程序需要显示提醒选项,如(2周前,1周前,3天前等),
  3. 如果所选日期是从现在开始的两天,那么我需要在选项前2周和1周前停用。
  4. 下面是使用但不起作用的代码,

     - (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;
            }
    }
    

1 个答案:

答案 0 :(得分:-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;    [notification setAlertBody:@" Your Reminder"];    [notification setFireDate:twoWeeksAgoDate];    [notification setTimeZone:[NSTimeZone defaultTimeZone]];    [application setScheduledLocalNotifications:[NSArray arrayWithObject:notification]];