Ios 删除包含日期的单元格时删除UILocalNotification
我正在制作提醒应用程序,其中有表视图,单元格中有日期,当该单元格日期变为今天时,UILocalNotification将触发。为此,我使用以下代码Ios 删除包含日期的单元格时删除UILocalNotification,ios,uilocalnotification,Ios,Uilocalnotification,我正在制作提醒应用程序,其中有表视图,单元格中有日期,当该单元格日期变为今天时,UILocalNotification将触发。为此,我使用以下代码 -(void)notification { // logic for local notification start NSDateFormatter *Form = [[NSDateFormatter alloc] init]; [Form setDateFormat:@"dd/MM/yyyy"]; UILoc
-(void)notification {
// logic for local notification start
NSDateFormatter *Form = [[NSDateFormatter alloc] init];
[Form setDateFormat:@"dd/MM/yyyy"];
UILocalNotification *notification = [[UILocalNotification alloc] init];
for (int i=0;i<_convertedBdates.count;i++)
{
NSDate *date =[Form dateFromString:[_convertedBdates objectAtIndex:i ]];
// NSLog(@"date%@",date);
if(notification)
{
notification.fireDate = date;
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = [NSString stringWithFormat:@"Today is %@\'s Birthday",[_combinedNameArray objectAtIndex:i]];
notification.alertAction = @"View";
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
// local notification logic ends here
}
-(作废)通知{
//本地通知启动逻辑
NSDateFormatter*Form=[[NSDateFormatter alloc]init];
[表格setDateFormat:@“dd/MM/yyyy”];
UILocalNotification*通知=[[UILocalNotification alloc]init];
对于(int i=0;iEdited):我最初的回答是错误的,因为我没有意识到操作系统会在您计划它们时复制UILocalNotification
s,而不仅仅是保留它们。因此
据我所知,有两种方法可以做到这一点
- 删除行时取消所有通知,然后重新安排其余的通知
如果您没有安排很多通知,那么这将更加高效,而且编写代码肯定会容易得多。(注意:我对工作中的低级事物了解不够,无法说明哪一种效率更高,但我猜两者之间的差异并不重要。)
每当删除一行时,只需调用
[[UIApplcation sharedApplication] cancelAllLocalNotifications];
然后适当地更新\u convertedbdate
,最后再次调用notification
方法,为仍然存在的事件重新安排新的本地通知
- 为本地通知创建唯一标识符
如果你能想出一个好的方法来制作那些唯一的标识符,如果你有很多的通知计划,这可能是更有效的方法。一种可能是使用通知发出的时间,前提是您可以保证不会同时发出两个通知。其他可能是通知的标签(同样,如果您可以确保唯一性)。无论您决定对您的唯一标识符使用什么,都可以通过在for循环外部添加以下内容来存储:
self.uniqueIDArray = [[NSMutableArray alloc] init];
(其中UniquedArray是类的NSMutableArray*@属性
),然后在安排通知之前:
[uniqueIDArray addObject:whateverObjectYouUseForTheUniqueID];
notification.userInfo = [[NSDictionary alloc] initWithObjects:whateverObjectYouUseForTheUniqueID
forKeys:@"uniqueID"];
然后,无论使用何种方法删除单元格,您都会执行以下操作:
uniqueIDToDelete = [self.uniqueIDArray objectAtIndex:indexOfCellBeingDeleted];
NSArray *scheduledNotifications = [[UIApplication sharedApplication] scheduledNotifications];
UILocalNotification *notifToDelete;
for (UILocalNotification *notif in scheduledNotifications) {
if ([[notif.userInfo objectForKey:@"uniqueID"] isEqual:uniqueIDToDelete]) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
}
}
[self.uniqueIDArray removeObjectAtIndex:indexOfCellBeingDeleted];
我认为您不应该使用单元格作为通知源。请改用数据模型。此外,您还可以轻松删除通知
更新
Model *model = [_array objectAtIndex:indexPath.row]; model.notificationID = // here you store created notification identifier
[[UIApplication sharedApplication] scheduledLocalNotifications] // contains all local notifications, you should search you need by ID and remove it using cancelLocalNotification method.
p.S.通知ID您可以存储在通知用户信息中
我不知道你能举个例子吗?我使用的是日期数组和我在表格视图中使用的相同数组。你能举个例子来演示一下吗?我刚启动iphone 2个月,所以除了取消通知外,一切都不清楚。我很抱歉,非常感谢你为陌生人花时间问候你。没问题。如果回答是有帮助的,即使他们没有解决你的问题,请考虑投票给他们。我不能投票,我没有足够的分数,但(我已经接受了你的答案:)
NSArray *notificationArray = [[UIApplication sharedApplication] scheduledLocalNotifications];
UILocalNotification *notif = [notificationArray objectAtIndex:indexPath];
[[UIApplication sharedApplication] cancelLocalNotification:notif];