1

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

#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

4

2 回答 2

1

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

于 2012-05-25T16:57:56.783 回答
0

添加:

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

到 HEFTTestDatabaseMasterVIewController 的 viewDidLoad 方法

工作并且还将在需要访问 managedObjectContext 的其他类中工作

于 2012-05-26T14:54:51.897 回答