Question

I'm successful in trying to set multiple reminders for a particular event at different times. However, I'm having trouble populating my TableView with reminders or UILocalNotifications with only the notifications that pertain to a particular event when I select an event from a tableview.

I realize that I have to filter local notifications. I have scheduled my local notifications with userInfo and of course alertBody, fireDate, and etc. How can I accomplish this? Most of the examples only show the entire list instead of filter certain ones.

My code:

@property (nonatomic, strong) NSMutableArray *searchResults;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //I want to be able to populate searchResults array on load so I can display the contents in my tableView
    NSArray *temp = [[UIApplication sharedApplication] scheduledLocalNotifications];

    for (int i=0; i<temp.count; i++) {
        UILocalNotification *localNotif = [temp objectAtIndex:i];
        self.searchResults = [NSMutableArray arrayWithObjects:localNotif.alertBody, nil];
        NSLog(@"My objects: %@", self.searchResults);
    }
}

I get this log:

2014-03-13 10:55:42.489 My App Name[392:60b] My objects: ( "Event name" )
2014-03-13 10:55:42.492 My App Name[392:60b] My objects: ( "Event name", )
2014-03-13 10:55:42.494 My App Name[392:60b] My objects: ( "Event name", ) 

So I have three notifications set at different times. The first two have the same event name and the third is different - so the local temp array has three elements. Well first I want to be able to add all three events or notifications in the searchResults array so it will print just one time, so it will look like :

2014-03-13 10:55:42.489 My App Name[392:60b] My objects: ( "Event name", "Event name", "Event name" )
Était-ce utile?

La solution

You should change your current code so that searchResults holds dictionaries, so you can store both the event name and other information (fire date, whatever) when you need it:

NSMutableArray *searchResults = [NSMutableArray array];

for (int i=0; i<temp.count; i++) {
    UILocalNotification *localNotif = [temp objectAtIndex:i];

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:localNotif.alertBody forKey:@"body"];

    [searchResults addObject:dict;
}

NSLog(@"My objects: %@", searchResults);
self.searchResults = searchResults;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top