1

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.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
kalafun
  • 3,512
  • 6
  • 35
  • 49

2 Answers2

2

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;
    }
}
bilobatum
  • 8,918
  • 6
  • 36
  • 50
  • Thanks a lot, that did it, I totally forgot that I have a navigationController before the otherController. – kalafun Dec 18 '13 at 10:19
0

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.

thorb65
  • 2,696
  • 2
  • 27
  • 37