I am trying to create a core data application and have had some success creating the entities from the app delegate directly. I just dumped some display to the NSLog to see that I was able to create an object and successfully fetch it later.
Now I want to move the managed object context to a viewcontroller that I made with the nib file, but it fails to run, because the managedObjectContext is set to zero. If I load the controller manually, I can set the managedObjectContext equal to the applicaiton's.
I tried to go to the interface builder and I add a view controller and set the class to MasterViewController, which is declared like this:
Code:
@interface MasterViewController : UIViewController {
NSManagedObjectContext *managedObjectContext;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@end
An in the app delegate, I include a pointer to the MasterViewController:
Code:
#import "MasterViewController.h"
@interface coreDataTestAppDelegate : NSObject <UIApplicationDelegate> {
NSManagedObjectModel *managedObjectModel;
NSManagedObjectContext *managedObjectContext;
NSPersistentStoreCoordinator *persistentStoreCoordinator;
UIWindow *window;
MasterViewController *myMasterViewController;
}
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet MasterViewController *myMasterViewController;
- (NSString *)applicationDocumentsDirectory;
@end
Finally in the interface builder I have MainWindow.xib and I add to it a viewcontroller with the class MasterViewController. I right click and set the view outlet to the window and the referencing outlet to be "myMasterViewController" in the app delegate. I know that I need to somehow link the managedObjectContext in myMasterViewController to the app delegate, but when I try to create the link, I don't see the managedObjectContext.
How do I initialize the managedObjectContext for a view controller in the interface builder to my app delegate?
Thanks for the help