Embed managedObjectContext in the initial viewController?

I am customizing the Core Data ( UIManagedDocument ) model in my AppDelegate and I was curious how I could add a reference to NSManagedObjectContext to my (storyboards) Initial viewController.

I could access it directly in viewControllers viewDidLoad using [ [[UIApplication sharedApplication] delegate] sampleDatabase] managedObjectContext] , and then pass it to additional controllers using prepareForSegue .

I'm just wondering if there is an initial segue (or mechanism) that would allow me to pass (or add) a managed ObjectContext to this initial viewController without having access to it, using the appDelegates sharing instance once inside the controller?

+4
source share
2 answers

When you use the storyboard, the source view controller is set as the rootViewController property of your delegate just before application:didFinishLaunchingWithOptions: called.

You should be able to use the listing on this link to set the property, assuming that by this time your master data structure will be created.

+4
source

Usually you just have a property on the view controller, which is NSManagedObjectContext, and just set this property right after initializing the viewController in the appDelegate application. myViewController.managedObjectContext = self.managedObjectContext

0
source

Source: https://habr.com/ru/post/1434833/


All Articles