Question

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.

Was it helpful?

Solution

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;
    }
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top