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;
UINavigationControllernotFavoriteMealsViewController. - Jakub Vano