我從AppDelegate將NSManagedObjectContext傳遞給ViewController。然後我從Core Data獲取結果。但是,NSManagedObjectContext在ViewDidLoad方法中始終爲零,而不是ViewDidAppear方法。屬性ViewDidLoad問題
我明白這兩種方法之間的區別,但我認爲我應該能夠從ViewDidLoad訪問屬性,我甚至注意到,在Apple的示例代碼中,他們這樣做。
我應該只是在ViewDidAppear中獲取?
- (void)viewDidLoad
{
[super viewDidLoad];
// This code crashings because my because my Context is nil
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1);
}
}
編輯:我通過像這樣
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
rootViewController.managedObjectContext = self.managedObjectContext;
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:rootNav, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
你在哪裏將'NSManagedObjectContext'傳遞給你的視圖控制器? – Macondo2Seattle
didFinishLaunchingWithOptions – Vikings
你可以請張貼代碼?您在didFinishLaunching中的視圖控制器的狀態...取決於您是使用故事板,還是自己初始化視圖控制器,無論它是初始視圖控制器等。 – Macondo2Seattle