简体   繁体   English

如何使用故事板与标签栏控制器作为初始控制器共享UIManagedDocument?

[英]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. 我想共享一个UIManagedObject以便使用相同的Managed Object Context,因此,如果用户在表视图上更新,则该数据也将针对该映射进行更新,因此无需更新两次。

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. 最初,我想到了对Tab Bar Controller进行子类化,并添加UIManagedDocument作为属性,然后将其传递给prepare Segue方法的每个控制器。 But i read that UITabBarController is not meant to be subclassed. 但是我读到UITabBarController并不打算被子类化。

Another approach could be creating a View Controller, adding the Managed Document as property, and a Tab Bar to it. 另一种方法是创建一个View Controller,将Managed Document as属性添加到它,并添加一个Tab栏。 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. 在此先感谢您,并致以最诚挚的问候。

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

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 我已经编写了解决方案,并在此处发布了代码: 具有单个共享UIManagedDocument的核心数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ios:如何使用StoryBoards从ViewController打开Tab Bar控制器 - ios: How open Tab Bar controller from ViewController using StoryBoards 与选项卡栏控制器一起使用多个情节提要 - Using Multiple Storyboards with Tab Bar Controller 故事板,标签栏控制器和主视图/详细视图 - storyboards, tab bar controller and a master/detail view 两个StoryBoards,一个标签栏控制器 - TWO StoryBoards, ONE Tab Bar Controller 使用 Storyboards 以编程方式设置初始视图控制器 - Programmatically set the initial view controller using Storyboards 使用情节提要板ioS5时从视图控制器移动到选项卡栏控制器 - Moving from a view controller to a tab bar controller when using storyboards ioS5 移动到另一个视图时标签栏控制器消失(iOS SDK,使用故事板) - Tab Bar controller disappearing when moving to another view (iOS SDK, Using storyboards) 设置选项卡栏的初始视图控制器 - Setting the initial view controller for tab bar iOS:如何在标签栏控制器的不同视图之间共享数据 - iOS: How to share data between different views of Tab Bar Controller 标签栏为初始视图控制器时,如何使“返回”按钮出现在导航控制器中 - How to make 'Back' button appear in Navigation Controller when Tab Bar is initial view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM