0

我有一个表格视图,如下所示,我确实使用相应的开关为每个单元格设置了提醒

-(IBAction)switchingbtn:(id)sender
{
    UISwitch *onoff = (UISwitch *) sender;
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    if(onoff.on)
    {
        NSLog(@"Shedule notification");


    int tagValue=[sender tag];

    NSMutableDictionary *dict = (NSMutableDictionary *)[alarmsArray objectAtIndex:tagValue];


    NSDate *firedate = [dict objectForKey:@"date"];
    NSLog(@"fire date is %@", firedate);
    localNotif.fireDate = firedate;

    localNotif.alertBody = @"Start Exercise";
    localNotif.applicationIconBadgeNumber = 0;

    // localNotif.timeZone =[NSTimeZone timeZoneForSecondsFromGMT:0];
    localNotif.timeZone = [NSTimeZone systemTimeZone];
    localNotif.repeatInterval = kCFCalendarUnitDay;


   //  [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; //**Not working**
    [localNotif release];

}

不,我需要为 ex 3rd swich 取消第 1 个通知取消第 3 个通知

else
    {
// Cancel a notification not works
      //  [[UIApplication sharedApplication] cancelLocalNotification:localNotif];

        [[UIApplication sharedApplication] cancelAllLocalNotifications];
    NSLog(@"cancel notification");
}
4

2 回答 2

1

到目前为止,取消单个通知的最佳方法是创建一个包含 userInfo 字典的通知,在此字典中,您可以为 id 键添加通知 ID 值。您跟踪通知 ID(存储在 plist、sql 数据库等中),当您需要删除通知时,您只需向 UIApplication 实例询问计划的通知并按 ID 过滤,当您找到匹配项时只需要发送该通知的取消方法。

于 2012-05-02T09:49:39.780 回答
0

这是你想要的代码

- (void)CancelExistingNotification {
//cancel alarm
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
    UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
    NSDictionary *userInfoCurrent = oneEvent.userInfo;
    NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"notificationID"]];
    if ([uid isEqualToString:[NSString stringWithFormat:@"%i",self.notificationID]])
    {
        //Cancelling local notification

        [app cancelLocalNotification:oneEvent];
        break;
    }
}

}

“self.notificationID”来自自定义对象上的一个属性,如alarmObject,它在应用程序范围内通过帮助NSUserDefaults加载。

于 2013-08-10T20:56:24.567 回答