I am learning to develop iPhone applications using iOS 5.
I am trying to create a contact list of sorts (to learn about Core Data).
I have a UITableView embedded in a UINavigationController (I have done this through Editor -> Embed In -> Navigation Controller).
At the end of my saveContact action I have the line:
[self.navigationController popViewControllerAnimated:YES];
To move from the contact creation form to the UITableView which lists all the contacts. The code that fetches the contacts is in viewWillAppear.
Problem is that when I pop the contact creation form the new contact does not show on the list.
I have found in Apple’s documentation that I should add the UINavigationControllerDelegate to my UITableViewController, but no success.
Any suggestions?
You need to implement the
navigationController:willShowViewController:animated:method of theUINavigationControllerprotocol in yourUITableViewControllerand set the delegate of the navigation controller the table view controller. In you implementation of the method, check if the view that will be shown is equal to your view, and if it is then fetch the results.If you really want to integrate Core Data with a table view, I would recommend looking into
NSFetchedResultsController. It simplifies handling updates to the backing data store, so if you later added background syncing functionality, the background thread could continuously update the store, and the NSFetchedResultsController could then handle updating the table for you. It would also make your current example easier, as all you’d have to do is sendsave:to Core Data and then the results controller would start updating your table, without using aviewWillAppear:method