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

只谈情不闲聊 提交于 2019-12-23 10:57:09

问题


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.


回答1:


- (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"];            
    }
}



回答2:


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



来源:https://stackoverflow.com/questions/9428126/how-to-share-a-uimanageddocument-using-storyboards-with-a-tab-bar-controller-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!