Sending the managedObjectContext to viewController crashes

1.4k views Asked by At

I am trying to send my managedObjectContext from my masterViewController to anotherController via a segue and I am always getting this error:

-[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x8d67c70
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x8d67c70'

I am getting the managedObjectContext from my appDelegate in the viewDidLoad() of the masterViewController like this:

AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
self.managedObjectContext = appDelegate.managedObjectContext;

I cant possibly think of anything I could have done wrong anymore, please help.

2

There are 2 answers

1
bilobatum On BEST ANSWER

I'm guessing that the view controller with the managedObjectContext property is embedded in a navigation controller. In the segue method, make sure you grab a reference to the correct view controller:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"MySegue"]) {

        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        MyViewController *vc = (MyViewController *)navController.topViewController;

        AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
        vc.managedObjectContext = appDelegate.managedObjectContext;
    }
}
0
thorb65 On

you call a setter method on UINAvigationController which only exists if you created a property managedObjectContext. So you would have to subclass the UINavigationConroller, create the property and then you should be able to set it from another class.