我使用的MainWindow.xib文件具有以下配置:
MainWindow.xib
- UITabBarController
- UINavigationBarController
- UITableViewControllerAlpha
- UINavigationBarController
- UITableViewControllerBeta
- UINavigationBarController
- UITableViewControllerCharlie
- UINavigationBarController
- 定义nib作为目标的主界面。
- 加载nav控制器和根视图控制器 c> / li>
- 在根视图控制器的 viewDidLoad 中从MOC提取
- 让应用程序委托在中设置控制器的managedObjectContext applicationDidFinishLaunching: / code>
- 在 viewDidLoad 中执行提取,而不是 initWithCoder 。
-
在 applicationDidFinishLaunching:中设置控制器的managedObjectContext,但在调用数据源方法之前不要提取数据。
-
编写一个类似 loadDataFromMOC:在您的控制器,这两个设置MOC和从它的抓取,并从 applicationDidFinishLaunching:
调用它。 - UITabBarController
- UINavigationBarController
- UITableViewControllerAlpha
- UINavigationBarController
- UITableViewControllerBeta
- UINavigationBarController
- UITableViewControllerCharlie
- UINavigationBarController
- Define nib as target's main interface.
- Load nav controller and root view controller in nib.
- Set root view controller's managedObjectContext property in applicationDidFinishLaunching:
- Fetch from MOC in root view controller's viewDidLoad
- Have the app delegate set the controller's managedObjectContext in applicationDidFinishLaunching:
- Do the fetch in viewDidLoad instead of initWithCoder.
Set the controller's managedObjectContext in applicationDidFinishLaunching:, but don't fetch data until your data source methods are called. In those methods, conditionally complete the fetch if it hasn't already been done.
Write a method like loadDataFromMOC: on your controller, which both sets the MOC and fetches from it, and call it from applicationDidFinishLaunching:.
但基于以下帖子: 为什么当我尝试在这些点上执行时,Core Data初始化失败? 和断点我已经做了,我得出结论,initWithCoder被用于初始化所有的UITableViewControllers,并且没有办法在应用程序生命周期的那一点可靠地使用managedObjectContext。
这是否意味着我必须抛弃我在MainWindow.xib中执行的所有简单设计工作和布局,并编程?使用核心数据,真的意味着不能使用NIB文件?
解决方案将Core Data,表视图和NIB一起使用没有问题。我假设你需要从你的表视图中获取一些对象从MOC,并且你有困难找到MOC时,你需要这样做。
这种方法工作对我来说:
所以,请先尝试:
如果这不起作用,您至少仍然有以下选项:
I am using a MainWindow.xib file that has the following configuration:
MainWindow.xib
But based on the following post: Why does Core Data initialization fail when I attempt to do it at these points? and the breakpoints that I've placed, I've come to the conclusion that initWithCoder is being used to init all of the UITableViewControllers and that there is no way to reliably use the managedObjectContext at that point in the applicaiton lifecycle.
So does this mean that I have to throw out all the "easy" design work and layout that I've performed in MainWindow.xib and do it programatically? Does using core data, really mean not being able to use NIB files? Or is there some middle-ground?
Please let me know, Thanks!
解决方案There's no problem using Core Data, table views, and NIBs together. I presume you need to fetch some objects from the MOC for your table view, and you're having problems finding the MOC when you need to do that.
This approach works for me:
So, try this first:
If that doesn't work, you still have at least these options:
更多推荐
在视图初始化时无法访问受管对象上下文
发布评论