I want to create a new UILocalNotification every time I enter a certain method. I would assume this would have to be done by reading from an array or something along this line but I cannot figure it out. How do I do such a thing dynamically without hardcoding something like the following:
-(void) createNotification
{
UILocalNotification *notification1;
}
Now I would like to be able to create notification2, notification3, etc etc each time I enter createNotification. For the specific reason that then I can cancel the appropriate notification without cancelling them all.
The following is what I have attempted, perhaps Im way off… maybe not. Either way if someone could provide some input, would be appreciated. Thanks!
-(void) AddNewNotification
{
UILocalNotification *newNotification = [[UILocalNotification alloc] init];
//[notificationArray addObject:newNotification withKey:@"notificationN"];
notificationArray= [[NSMutableArray alloc] init];
[notificationArray addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:newNotification,@"theNotification",nil]];
}
You are almost there: using an array is certainly the right thing to do! The only problem is that you keep creating a new instance of the array every time you go through your
AddNewNotificationmethod. You should makenotificationArrayan instance variable, and move its initialization codenotificationArray= [[NSMutableArray alloc] init];to the designated initializer of the class wherenotificationArrayis declared.If you would like to give each notification that you insert an individual key by which you can find it later, use
NSMutableDictionaryinstead ofNSMutableArray. Re-write theAddNewNotificationmethod as follows:When you call the
addNewNotificationWithKey:method, you’d be able to provide a key for the newly added notification, for exampleand so on.