我正在关注本教程:http ://maybelost.com/2011/12/tutorial-storyboard-app-with-core-data/ 尝试在类之间传递 managedObjectContext 时出现此错误:
2012-04-23 09:53:13.969 PictureList[10287:fb03] -[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x6d20a80
2012-04-23 09:53:13.992 PictureList[10287:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setManagedObjectContext:]: unrecognized selector sent to instance 0x6d20a80'
*** First throw call stack:
(0x16ab022 0x183ccd6 0x16accbd 0x1611ed0 0x1611cb2 0x1c05 0x14386 0x15274 0x24183 0x24c38 0x18634 0x1595ef5 0x167f195 0x15e3ff2 0x15e28da 0x15e1d84 0x15e1c9b 0x14c65 0x16626 0x1afd 0x1a65)
terminate called throwing an exception(lldb)
该错误似乎有点模糊,但我怀疑 UINavigationController 是导致此问题的原因。当我运行它时,该应用程序立即崩溃。
这是我的 Appdelegate.m 的代码,我试图将 Context 传递给我的初始控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Pass the managed object context to the root view controller (the login view)
LoginViewController *rootView = (LoginViewController *)self.window.rootViewController;
rootView.managedObjectContext = self.managedObjectContext;
....
}
return YES;
}
然后我试图通过一个 segue 从我的 LoginViewController 传递到下一个类。登录视图控制器.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UINavigationController *navController = (UINavigationController *)[segue destinationViewController];
PictureListMainTable *piclist = (PictureListMainTable *)[[navController viewControllers] lastObject];
piclist.managedObjectContext = managedObjectContext;
}
我尝试的另一件事是将上下文传递给 UINavigationController:
// This code was customized to handle the navigation controller
// Pass the managed object context to the root view controller (the login view)
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
LoginViewController *rootView = (LoginViewController *)navigationController.topViewController;
rootView.managedObjectContext = self.managedObjectContext;
使用这种方法,我至少能够让应用程序加载到登录屏幕,但是当我尝试 segue 时,应用程序会崩溃。我该如何解决这个问题?
编辑:好的,找到问题了。在教程中,我认为导航是在登录控制器之前嵌入的,但导航控制器实际上位于 LoginViewController 和 PictureListMainTable 之间。