0
votes

I have a complex project (I didn't write it). I had to add a new screen before the first screen in the storyboard. In the App Delegate they hardcoded getting get the View Controller from the storyboard. My attempt to rewrite this code crashes.

Original code (AppDelegate.m):

    UINavigationController *favoriteMealsNavigationController = [[tabBarController viewControllers] objectAtIndex:1];
    FavoriteMealsViewController *favoriteMealsViewController = [[favoriteMealsNavigationController viewControllers] objectAtIndex:0];
[favoriteMealsViewController setManagedObjectContext:[self managedObjectContext]];

My attempt:

    FavoriteMealsViewController *favoriteMealsViewController = (FavoriteMealsViewController *)[self.mainStoryboard instantiateViewControllerWithIdentifier:@"mealsScene"];
//crashes here:
    [favoriteMealsViewController setManagedObjectContext:[self managedObjectContext]];

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

*favoriteMealsViewController is non null self.managedObjectContext is non null

This is the code in FavoriteMealsViewController.h

@interface FavoriteMealsViewController : UIViewController <NSFetchedResultsControllerDelegate, UIActionSheetDelegate, MealAddDelegate>

/** The managed object context associated with the Core Data Model. */
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;


@end

This is the pertinent code in FavoriteMealsViewController.h:

@synthesize managedObjectContext = _managedObjectContext;
1
Your view in storyboard is called "mealsScene"? you set this name? - ErasmoOliveira
Error message suggests that you are instantiating UINavigationController not FavoriteMealsViewController. - Jakub Vano

1 Answers

0
votes

This means that your attempt to instantiate the FavoriteMealsViewController with this identifier: @"mealsScene" is in fact instantiating a UINavigationController instead. Go to your storyboard and make sure the ID you are using is for the FavoriteMealsViewController and not a navigation controller and it should work