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

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

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

我在 DataManager 中调用我的方法来循环通过该 MyObject 实例来安排该 MyObject 的通知。

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

[[UIApplication sharedApplication] scheduledNotifications];

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

谢谢!

4

2 回答 2

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;
}
于 2012-04-12T05:01:39.590 回答
0

使用类的userInfoNSDictionary类型)属性UILocalNotification来区分通知

看到这个链接

于 2012-04-12T05:01:15.217 回答