使用目标c禁用提醒

时间:2013-12-30 06:57:07

标签: objective-c reminders

我正在开发一个药物提醒应用程序。用户从日期选择器中插入药物名称和时间,然后为每个选择器创建一个提醒。

我的问题:当用户想要编辑任何药物的上药时间时,如何删除提醒?

这是我保存提醒的代码:

 // Get the current date

    NSDate *pickerDate = [datePicker date];
   // Break the date up into components
    NSDateComponents *dateComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit )      fromDate:pickerDate];

    NSDateComponents *timeComponents = [calendar components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:pickerDate];

    NSDateComponents *dateComps = [[NSDateComponents alloc] init];

    [dateComps setDay:[dateComponents day]];

    [dateComps setMonth:[dateComponents month]];

    [dateComps setYear:[dateComponents year]];

    [dateComps setHour:[timeComponents hour]];

//  // Notification will fire in one minute

    [dateComps setMinute:[timeComponents minute]];

[dateComps setSecond:[timeComponents second]];

    NSDate *itemDate = [calendar dateFromComponents:dateComps];

EKEventStore *eventStore = [[EKEventStore alloc] init];
if([eventStore respondsToSelector:@selector(requestAccessToEntityType:completion:)])

{
    // iOS 6 and later

    // asks user to allow application to use his device calendar

    [eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted, NSError *error)

     {
        if (granted)
        {
            EKReminder *reminder = [EKReminder reminderWithEventStore:eventStore];

             reminder.title = [NSString stringWithFormat: @"وقت الدواء"] ;

             reminder.calendar = [eventStore defaultCalendarForNewReminders];

            // NSDate *date = itemDate;

             EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:itemDate];
             [reminder addAlarm:alarm];
             [EventIDArray addObject:alarm];

             NSError *error = nil;
             [eventStore saveReminder:reminder commit:YES error:&error];

             if(error)

                 NSLog(@"unable to Reminder!: Error= %@", error);
        }

     }];

}

// iOS < 6

else

{        EKReminder *提醒= [EKReminder reminderWithEventStore:eventStore];

    reminder.title = [NSString stringWithFormat: @"وقت الدواء"] ;

    reminder.calendar = [eventStore defaultCalendarForNewReminders];

    NSDate *date = itemDate;

    EKAlarm *alarm = [EKAlarm alarmWithAbsoluteDate:date];

    [reminder addAlarm:alarm];
    [EventIDArray addObject:alarm];
    NSError *error = nil;

    [eventStore saveReminder:reminder commit:YES error:&error];

    if(error)

        NSLog(@"unable to Reminder!: Error= %@", error);

我使用删除提醒但提醒仍然有效。

[eventStore removeReminder:[EventIDArray objectAtIndex:row] commit:YES error:&error];

谢谢,我感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

要有效取消提醒(EKReminder),您应将已完成的属性设置为YES。

这会自动将该EKReminder的completionDate设置为当前日期。

Apple API Documentation for EKReminder

如上文所述,您只需将该属性设置为YES即可取消提醒。

对我来说,我想这会是这样的:

(EKReminder *)[EventIDArray objectAtIndex:row].completed = YES;

假设对象从:

返回
[EventIDArray objectAtIndex:row] 

是您要取消的EKReminder对象实例。

答案 1 :(得分:0)

您需要像这样强制转换从数组返回的对象。

(EKReminder *)[EventIDArray objectAtIndex:row].completed = YES;