如何跟踪当前安排的UILocalNotifications

时间:2012-04-12 03:40:05

标签: iphone uilocalnotification

MyObject : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSArray *notificationsArray;

我在tableView中有一个MyObjects数组,可以根据名称,通知时间等进行编辑。目前,我已经设置好了,所以当用户按下Save时,当前的MyObject会保存到我的DataManager的myObjectArray。 / p>

DataManager : NSObject
@property (nonatomic, strong) NSMutableArray *myObjectArray;

我在DataManager中调用我的方法来遍历该MyObject实例,以安排该MyObject的通知。

我认为这没问题,直到用户点击其中一个MyObjects来编辑时间,然后我才需要重新安排该对象的通知。我知道你可以得到

[[UIApplication sharedApplication] scheduledNotifications];

但是从这一点来看,我不知道哪个通知针对哪个对象。因此,在这种情况下,最好取消整个应用程序的所有通知,然后在DataManager中为每个MyObject实例循环遍历myObjectArray,并为每个对象安排通知吗?

谢谢!

2 个答案:

答案 0 :(得分:6)

您可以使用userInfo属性将自定义数据与通知相关联。像这样构建:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.userInfo = [NSDictionary dictionaryWithObject:@"myNotificationName" forKey:@"name"];

然后这样查看:

 - (UILocalNotification *)notificationNamed:(NSString *)name {

    for (UILocalNotification *notification in [[UIApplication sharedApplication] scheduledLocalNotifications]) {
        NSString *notificationName = [notification.userInfo valueForKey:@"name"]; 
        if ([notificationName isEqualToString:name])
            return notification;
    }
    return nil;
}

答案 1 :(得分:0)

使用userInfo类的NSDictionaryUILocalNotification类型)属性来区分通知查看此link

相关问题