开发者

How do I reference the managedObjectContext in my App Delegate from my View Controller?

I know there are other questions covering this topic, but none seem to fit exactly what I'm experiencing, hence the new question.

I have an app which is a UITabBarController, I have defined two subviews

Both tabs have their Class attribute in the Identity Inspector set to UINavigationController.

Both subviews are Class UIViewController and contain MKMapView objects.

I am trying to integrate Core Data with the objective being that I can use Core Data to store information about object I want to place on the map.

I have my UITabBarController defined as 'rootController' in my Delegate header file. I also have the managedObjectModel, managedObjectContext and persistentStoreCoordinator properties defined there.

In the Delegate implementation I have the standard accessor methods for the above properties and I have rootController defined as follows:

开发者_Python百科
- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window addSubview:rootController.view];
[window makeKeyAndVisible];

}

In my view controllers for the child views, I have defined my managedObjectContext and synthesized it.

Now for my problem, I cannot get the compiler to allow me to reference the managedObjectContext in the App Delegate from the View Controllers.

I tried the following in the applicationDidFinishLaunching method:

firstView.managedObjectContext = self.managedObjectContext;

But I just get the following error:

Accessing unknown 'setManagedObjectContext:' class method

Can anyone help me figure this out?

Thanks


Update:

To add to my question, I'll head off some answers by providing more detail

I have an @Class declaration in my appdelegate.h file I have a #import statement in my appdelegate.m file for the firstView.h file I have declared my firstView as follows in my appdelegate.h file

FirstView *firstView;


How is firstView declared and assigned?

It should be a reference to an object with a property named "managedObjectContext".


I've ran into this same problem, i'll share my solution.

First you need a reference to the Nav Controller in the Tab Bar in the nib file, make sure you connect it up.

IBOutlet UINavigationController *navigationController;

Then, get the Controller as recommended in the support docs and send it the managedObjectContext:

SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;

Alex (from another post) is right, "You should generally stay away from getting shared objects from the app delegate. It makes it behave too much like a global variable, and that has a whole mess of problems associated with it."

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜