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:
- 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.
- to add a managed object context property to the view controller that launches the tab view controller? This is certainly possible using the
presentingViewControllerproperty in each of the destination view controllers but does not seem to be what was originally intended. - 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.
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: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.