简体   繁体   中英

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.
  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. 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)
  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. 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:

- (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. For your purpose, that just means handing them the managed object context that they should operate on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM