我正在尝试做一个简单的核心数据应用程序。我在应用程序委托中有以下代码:
#import <UIKit/UIKit.h>
@interface HEFTAppDelegate : UIResponder <UIApplicationDelegate>{
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
然后在实现中
#import "HEFTAppDelegate.h"
#import "HEFTMasterViewController.h"
#import "HEFTTestdatabaseMasterViewController.h"
#import "HEFTData_Manager.h"
@implementation HEFTAppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
//ipad init
} else {
HEFTTestdatabaseMasterViewController *testDatabaseMasterViewController = [[HEFTTestdatabaseMasterViewController alloc]initWithStyle:UITableViewStylePlain];
HEFTData_Manager *dataManager = [[HEFTData_Manager alloc]init];
self.managedObjectContext = [dataManager mainObjectContext];
// Pass the managed object context to the view controller.
testDatabaseMasterViewController.managedObjectContext = self.managedObjectContext ;
}
return YES;
}
HEFTData_Manager 类设置上下文等
查看调试器,上下文似乎在应用程序委托中正确设置,并且当
testDatabaseMasterViewController.managedObjectContext = self.managedObjectContext ;
发出消息。
但是,当表视图出现时 HEFTTestdatabaseMasterViewController managedObjectContext 为 NULL
任何关于正在发生的事情和指点的想法将不胜感激。
调试器输出
在:
testDatabaseMasterViewController.managedObjectContext = self.managedObjectContext ;
testDatabaseMasterViewController HEFTTestdatabaseMasterViewController
* 0x06d74b50 UITableViewController UITableViewController managedObjectContext NSManagedObjectContext * 0x06d778e0
fetchedResultsController NSFetchedResultsController * 0x00000000
在:
- (void)viewDidLoad
{
[super viewDidLoad];
// NSManagedObjectContext *context = self.managedObjectContext;
NSLog(@"context : %@",self.managedObjectContext);
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
self HEFTTestdatabaseMasterViewController *const 0x06d705a0 UITableViewController UITableViewController
UIViewController UIViewController
_tableViewStyle UITableViewStyle UITableViewStylePlain _keyboardSupport id 0x00000000 _staticDataSource id 0x00000000 _tableViewControllerFlags managedObjectContext NSManagedObjectContext * 0x00000000 fetchedResultsController NSFetchedResultsController