在具有核心数据的表视图中按行设置应用程序徽章编号
我正在开发一个使用核心数据来保存和检索数据的应用程序。
现在我想在主屏幕的应用程序图标上添加徽章编号,但我遇到了一些问题...
我尝试了这段代码:
NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];
这段代码不起作用,但我不知道该怎么做...
我必须编写什么代码以及在哪里?在 AppDelegate 或 RootViewController 中?以及用什么方法呢?
感谢大家的帮助!
I'm developing an app that uses Core Data for save and retrieve data.
Now I would like to add a badge number on app's icon in the home screen but I have some problems...
I tried this code:
NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];
This code don't work but I don't know how to do this...
What code I must write and where? Inside AppDelegate or RootViewController? And in which method?
Thanks all for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是有效的节号;它们从 0 到 numberOfSections-1。
That's not a valid section number; they go from 0 to numberOfSections-1.
这将返回 1 或更多。不是要插入
[[[self fetchedResultsController]sections] objectAtIndex:section]
的值。您应该给它一个实际的部分(零个或多个)而不是部分的总数。此外,您可以在
sectionInfo
变量的赋值上放置一个断点,并确保不会返回 nil。您还可以在sectionInfo 之后进行调试,以确保您得到有意义的信息。如果为零或返回零,则您的徽章将不会显示。
This is going to return back 1 or more. Not a value to be plugging into
[[[self fetchedResultsController] sections] objectAtIndex:section]
. You should be giving it an actual section (zero or more) not the total number of sections.In addition you can put a breakpoint on your assignment of the
sectionInfo
variable and make sure you are not getting a nil back. You can also put a debug after the sectionInfo to make sure you are getting something meaningful back.If it is nil or returning zero then your badge will not show.