我想我在这里有一些简单的错误。我正在尝试将 AppDelegate 的 managedObjectContext 传递给两个视图控制器。就这个学习演示而言,两个 ViewController 是紧挨着的。(在我的真实应用中,它们会相距很远。)
这有效:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
Page1ViewController * page1VC = [[navigationController viewControllers] objectAtIndex:0];
page1VC.managedObjectContext = self.managedObjectContext;
return YES;
}
但是当我添加第二个视图控制器 - objectAtIndex:1 时,它不会:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
Page1ViewController * page1VC = [[navigationController viewControllers] objectAtIndex:0];
page1VC.managedObjectContext = self.managedObjectContext;
Page2ViewController *page2VC = [[navigationController viewControllers] objectAtIndex:1];
page2VC.managedObjectContext = self.managedObjectContext;
return YES;
}
我收到此错误:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
我敢肯定它接近正确!?如果有人可以提供帮助将不胜感激。(顺便说一句,我也将使用 segues 来传递 managedObjectContext!)