views:

55

answers:

2

Hi all,
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!

A: 
NSInteger section = [[self tableView] numberOfSections];

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.

Marcus S. Zarra
A: 
NSInteger section = [self.tableView numberOfSections];

That's not a valid section number; they go from 0 to numberOfSections-1.

tc.
Thanks a lot! I tried with this code: NSInteger section = [self.tableView numberOfSections] - 1; id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; [[UIApplication sharedApplication] setApplicationIconBadgeNumber:[sectionInfo numberOfObjects]]; And works fine! :) I put it in viewWillAppear method because the number gets up to date every time I add a new object! Thank! ;)
Matthew