Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top