我正在尝试将我的app委托中的上下文传递给我的rootview,然后传递到我的辅助视图。辅助视图是真正加载所有数据的地方,rootview只是几个关键点的快速概要。当我在appDelegate中实例化上下文时,我会检查它是否为nil:
NSManagedObjectContext *context = [self managedObjectContext];
if (!context) {
//Handle error
}
if (context == nil) {
NSLog(@"Context is nil in appdelegate");
}
else {
NSLog(@"Context is set in appdelegate");
}
然后将rootViewController的上下文设置为
rootViewController.context = context;
然后进行相同的检查,两者都显示为零。然后我推动视图。然后我检查是否在viewDidLoad期间设置了上下文并且它是nil。 rootViewController:
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *context;
NSMutableArray *rootViewContentArray;
}
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSManagedObjectContext *context;
@end
#import "RootViewController.h"
#import "DetailViewController.h"
#import "SwapViewController.h"
#import "Swap.h"
@implementation RootViewController
@synthesize fetchedResultsController, context;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
//initialize array for rootView
rootViewContentArray = [[NSMutableArray alloc] init];
NSArray .....set array values....
//check context
if (context == nil){
NSLog(@"RootViewcontroller context is nil during viewdidload");
}
if (!(context == nil)){
NSLog(@"RootViewcontroller context is set during viewdidload");
}
//fill array
.........
}
我错过了一些小事,或者我在这里有一个根本的误解?此外,在我看来,如果我在视图上设置变量我创建它仍应在视图加载时实例化。我正在浏览示例应用程序并使用Apress在iPhone上开发的书籍,并尝试使用我迄今学到的知识创建简单的应用程序。核心数据应用程序似乎过于简单或超出我的想象,我正试图跳跃。任何帮助将不胜感激!感谢。
答案 0 :(得分:0)
if(managedObjectContext == nil)
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = [appDelegate managedObjectContext];
}
答案 1 :(得分:0)
我发现我做错了什么,我没有在我的appDelegate中将rootViewController设置为我的navigationController。我现在的问题是为什么我的程序运行正常,直到我尝试访问rootview中的Core Data对象?为什么我创建的rootView会通过调用
来推送 [window addSubview:[navigationController view]];
以前我的应用中根本没有设置导航控制器:
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = [self managedObjectContext];
if (rootViewController.managedObjectContext == nil) {
NSLog(@"RootViewcontroller context is nil before view is pushed");
}
if (!(rootViewController.managedObjectContext == nil)){
NSLog(@"RootViewcontroller context is SET before view is pushed");
}
// Add the navigation controller's view to the window and display.
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
现在代码为
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];
rootViewController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
if (rootViewController.managedObjectContext == nil) {
NSLog(@"RootViewcontroller context is nil before view is pushed");
}
if (!(rootViewController.managedObjectContext == nil)){
NSLog(@"RootViewcontroller context is SET before view is pushed");
}
[rootViewController release];
// Add the navigation controller's view to the window and display.
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
一切都很完美!?