简体   繁体   English

将核心数据与情节提要中的选项卡栏控制器一起使用

[英]using Core Data with tab bar controller in storyboard

I'm calling a tab bar controller modally from a view controller to implement a range of additional controls and inputs that the user can configure. 我从视图控制器以模态方式调用选项卡栏控制器,以实现用户可以配置的一系列其他控件和输入。 In storyboard this is easy to do but how can I best pass a Core Data managed object context to the view controllers hosted by the tab controller? 在情节提要中,这很容易做到,但是如何最好地将核心数据托管对象上下文传递给选项卡控制器托管的视图控制器? What is the best design approach here: 最好的设计方法是什么?

  1. to forget storyboard and do this part of the app in code? 忘记情节提要,并在代码中完成应用程序的这一部分? That is straightforward. 那很简单。 I simply pass the managed object context to each view before I add them to the tab controller. 我只是将托管对象上下文传递给每个视图,然后再将它们添加到选项卡控制器。
  2. to add a managed object context property to the view controller that launches the tab view controller? 向启动选项卡视图控制器的视图控制器添加托管对象上下文属性? This is certainly possible using the presentingViewController property in each of the destination view controllers but does not seem to be what was originally intended. 在每个目标视图控制器中使用presentingViewController属性,当然可以做到这一点,但似乎并非最初意图。
  3. communicate directly via some property of the root view controller? 通过根视图控制器的某些属性直接通信? I have seen references to this on the web but am not sure about this. 我在网上看到过对此的引用,但对此不确定。

Appart from the managed data context, nothing else is required appart from the dismissModalViewController message back to return to the original view. 来自托管数据上下文的Appart,不需要任何其他操作, dismissModalViewControllerdismissModalViewController消息中的dismissModalViewController返回到原始视图即可。 Everything else is managed via Core Data. 其他所有内容均通过核心数据进行管理。

A couple of options: 有两个选择:

  1. Pass the managed object context during prepareForSegue (you have to access the tab view controller's viewControllers array to get hold of your individual view controllers) prepareForSegue期间传递托管对象上下文(您必须访问选项卡视图控制器的viewControllers数组以获取各个视图控制器)
  2. Structure your app such that the core data stack is available globally, either from the application delegate class or a separate singleton. 对应用程序进行结构设计,以便可以从应用程序委托类或单独的单例全局获取核心数据堆栈。 The view controllers can then ask for the managed object context when they need it. 然后,视图控制器可以在需要时询问被管理对象的上下文。
  3. Possibly do some abuse of delegates where you set some object as the tab bar controller's delegate that also happens to hold the managed object context - this will then be available from all the view controllers in the tab bar controller. 可能会滥用一些委托,在这些委托中您将某些对象设置为选项卡栏控制器的委托,而该委托恰好包含托管对象的上下文-这样,选项卡栏控制器中的所有视图控制器都可以使用该委托。 This has only just occurred to me and is probably a bad idea. 这只是我想到的,可能不是一个好主意。

By the time your main view controller gets a -prepareForSegue: message, the tab bar controller and the view controllers that it manages will have already been created. 到主视图控制器收到-prepareForSegue:消息时,将已经创建了标签栏控制器及其管理的视图控制器。 You can get the tab bar controller from the segue itself, and then get the array of view controllers from the tab bar controller like so: 您可以从segue本身获取标签栏控制器,然后从标签栏控制器获取视图控制器数组,如下所示:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITabBarController *tbc = [segue destinationViewController];
    NSArray *controllers = [tbc viewControllers];
    NSLog(@"View Controllers: %@", controllers);
}

Now, you'll want to do a little error checking to make sure that the destination controller really is the tab bar controller, but you can replace the NSLog() with code to configure the controllers however you like. 现在,您将需要进行一些错误检查,以确保目标控制器确实是选项卡栏控制器,但是您可以将NSLog()替换为代码以配置所需的控制器。 For your purpose, that just means handing them the managed object context that they should operate on. 为了您的目的,这仅意味着向他们提供了应对其进行操作的托管对象上下文。

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

相关问题 使用标签栏控制器的核心数据添加记录 - Adding Record using Core Data with Tab Bar Controller 故事板 - ViewController到标签栏控制器 - Storyboard - ViewController to tab bar controller iphone标签栏控制器和核心数据 - iphone tab bar controller and core data 标签栏控制器(故事板模板)和AppDelegate - Tab Bar Controller (storyboard template) and AppDelegate 在Storyboard xCode中从View Controller切换到Tab Bar Controller - Switch from View Controller to Tab Bar Controller in a Storyboard xCode 快速更改情节提要标签栏图标 - change storyboard tab bar icon using swift 如何使用情节提要创建的选项卡栏控制器在视图之间传递对象 - How to pass object between views with a storyboard Created Tab Bar Controller 故事板选项卡栏控制器视图方向横向/纵向 - Storyboard tab bar controller view orientation landscape/portrait 如何确保在标签栏控制器上切换标签之间进行核心数据检索? - How do I ensure Core Data retrieval between switching tabs on a tab bar controller? 根据故事板中导航控制器的不同按钮调用相同标签栏控制器的特定选项卡 - Calling particular tabs of same tab bar controller based on different buttons from navigation controller in storyboard
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM