从视图控制器传递后,传递的托管对象上下文为null

时间:2012-05-25 15:14:50

标签: objective-c ios

我正在尝试做一个简单的核心数据应用。我在app delegate中有以下代码:

#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类设置上下文等

查看调试器,上下文似乎在app delegate和

中正确设置
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 * 0x00000000

2 个答案:

答案 0 :(得分:1)

您应该在managedObjectContext类的viewDidLoad方法中分配HEFTTestdatabaseMasterViewController属性,或者如果这是您不应编辑的库文件,只需将其子类化并设置它在自定义类的viewDidLoad中。

答案 1 :(得分:0)

添加了:

id appDelegate =(id)[[UIApplication sharedApplication] delegate];     self.managedObjectContext = [appDelegate managedObjectContext];

到HEFTTestDatabaseMasterVIewController的viewDidLoad方法

已完成工作,也可以在需要访问managedObjectContext

的其他类中工作