0

I am trying the "pass the baton" method for passing the managedObjectContext (MOC) thru multiple views. I have it successfully passed to the rootViewController. From there I move to a tabBarController via presentViewController. I can't seem to find a way to pass the MOC when the tabBarController is pushed.

AppDelegate.m

UIViewController *navigationController = (UIViewController *)self.window.rootViewController;
MyViewController *controller = (MyViewController *) navigationController;
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;

The main view controller is basically a start up screen that will kick you into a login screen or if you are already logged in, to the tabBarController. Below is where I transition to the tabBarController from within the viewDidAppear method.

MyViewController.m

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:@"mainTabVC"];
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];

The tabBarController in the storyboard has the identifier "mainTabVC".

I've tried lines like

MyTabBarController.managedObjectContext = self.managedObjectContext;

but I get the error Property 'MOC' not found on object of type MyTabBarController even though I do have the property declared in MyTabBarController.h

Could someone show me a line of code that I can throw in this segue to push the MOC to the tab bar controller.

BTW- I'm utilizing RestKit in this app if that changes the way I should be handling this please let me know.

*****Solution********

To make things clear for any other new guys with the same question. I went from this:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:@"mainTabVC"];
    [mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:mainTabVC animated:NO completion:nil];

To this:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
    MyTabBarViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:@"mainTabVC"];
     mainTabVC.managedObjectContext = self.managedObjectContext;
    [mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:mainTabVC animated:NO completion:nil];

Notice the assignment in the third line and using MyTabBarViewController instead of UIViewController in the second line. BIG thanks again to rdelmar!

4

1 回答 1

0

您的代码有些混乱。MyTabBarController 是类吗?看起来 mainTabVC 是您的实例。您应该使用它而不是类,并且您应该在将 mainTabVC 实例化为 MyTabBarController 而不是 UITabBarController 时更改类型。您也不需要按照您的方式获取故事板,您可以使用 self.storyboard。

    MyTabBarController *mainTabVC = [self.storyboard instantiateViewControllerWithIdentifier:@"mainTabVC"];
    mainTabVC.managedObjectContext = self.managedObjectContext;
    [mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
    [self presentViewController:mainTabVC animated:NO completion:nil];
于 2013-05-03T02:50:45.313 回答