我正在开发一个使用核心数据来保存和检索数据的应用程序。

现在,我想在主屏幕上的应用程序图标上添加一个徽章号,但我有一些问题...
我尝试了此代码:

NSInteger section = [self.tableView numberOfSections];
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects];

该代码不起作用,但我不知道该怎么做...
我必须编写什么代码?在哪里?内部AppDelegate或rootviewController?在哪种方法中?

谢谢大家的帮助!

有帮助吗?

解决方案

NSInteger section = [self.tableView numberOfSections];

那不是有效的部分编号;他们从0到数字1。

其他提示

NSInteger section = [[self tableView] numberOfSections];

这将返回1或更多。没有值得插入的价值 [[[self fetchedResultsController] sections] objectAtIndex:section]. 。您应该给它一个实际的部分(零或更多),而不是部分总数。

此外,您可以在分配的分配中分解 sectionInfo 可变,并确保您没有回来。您还可以在SectionInfo之后进行调试,以确保您获得有意义的东西。

如果是零或返回零,则您的徽章将不会显示。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top