Reputation: 943
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:
presentingViewController
property in each of the destination view controllers but does not seem to be what was originally intended.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.
Upvotes: 1
Views: 756
Reputation: 125007
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.
Upvotes: 0
Reputation: 119272
A couple of options:
prepareForSegue
(you have to access the tab view controller's viewControllers
array to get hold of your individual view controllers) Upvotes: 1