Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a UIManagedDocument using Storyboards with a Tab Bar Controller as initial controller?

My application uses locations data and presents it both as a table and in a map.

It starts with a Tab Bar Controller, each of it's views is a Navigation Controller (one for the table view, one for the map, etc...).

I want to share a UIManagedObject in order to use the same Managed Object Context so if the user updates at the table view, the data also gets updated for the map, so there is no need to update twice.

Originally i thought of subclassing the Tab Bar Controller and adding a UIManagedDocument as a property, and just passing it to each controller on the prepare for segue method. But i read that UITabBarController is not meant to be subclassed.

Another approach could be creating a View Controller, adding the Managed Document as property, and a Tab Bar to it. But i think that my storyboard would be unclear or inconsistent by showing some relationships graphically and others just in code.

Which one is the appropriate? Or is there a better way to do it?

Thanks in advance and best regards.

like image 984
Armando Avatar asked Feb 24 '12 09:02

Armando


2 Answers

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showGuide"]) 
    {
        UITabBarController *tabBarController = (UITabBarController *)[segue destinationViewController];
        for (id vc in tabBarController.viewControllers) {      
            [vc setValue:_document forKey:@"document"];            
    }
}
like image 185
Shmidt Avatar answered Sep 29 '22 23:09

Shmidt


I ran into this issue too and I settled on a separate document handler class that provides access to the loaded document via a block.

[[MYDocumentHandler sharedDocumentHandler] performWithDocument:^(UIManagedDocument *document) {
    // Do stuff with the document, set up a fetched results controller, whatever.
}];

I've written up my solution and posted the code here: Core Data with a Single Shared UIManagedDocument

like image 45
Justin Driscoll Avatar answered Sep 29 '22 23:09

Justin Driscoll