我从事 iOS 开发已有 3 个月了,目前我正在使用 tableviewcontrollers 和 viewcontrollers 使用多个 View Controller (~12)在 Xcode 8.2 中构建主从应用程序。
我已经利用基础知识在单个 View Controller 中保存和获取核心数据,并将数据推送到另一个 View Controller 。但这就是我被困的地方。许多关于 Apple 文档的教程和多次审查我仍然坚持如何跨多个 View Controller (以及因此多个 swift 文件)使用 CoreData。
如果有人知道涵盖该主题的优秀教程,将不胜感激。
或者,如果您可以推荐以这种方式使用 CoreData 的最佳方式,我们也将不胜感激。
应用程序的基本结构是:
Project List (in Master View Controller)
Add Project (Modal form)
Project Sections (in Master View Controller)
Project Sections Detail (in multiple Detail View Controllers)
我遇到的问题是:
By the time you drill down to the Project Sections Details level, the connection to the CoreData has dropped out (despite my efforts to pass data forward) and I can't populate cells/textfields/etc. with existing data
Atempts to save at this point generate a new CoreData object rather than updating the existing one.
向前和向后传递数据是使用 CoreData 的方式吗?或者有没有一种方法可以创建单个托管对象上下文(即在父类(super class)中?)并与之进行交易?
提前致谢。
最佳答案
很抱歉延迟回复。我需要的信息非常广泛,因此不得不跳过大量很快就会失去连贯性的代码。所以我最终解决了我自己的问题。
A) Web 上关于 Core Data 的最佳教程是 TutsPlus 上的这个.从概念上和实践上涵盖所有基础知识和高级功能。我怎么推荐都不为过。
B) 我发现跨多个 View Controller 使用 Core Data 的解决方案很容易实现,就是将需要重复访问的变量移动到全局变量中。例如在我的例子中:
/------------------------------------/
导入 UIKit
导入核心数据
var fetchedResultsController: NSFetchedResultsController
var managedObjectContext: NSManagedObjectContext?
类 MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate {
//代码
/------------------------------------/
希望对其他人有所帮助!
PS 代码片段在触摸屏上已关闭,因此我对格式表示歉意。
关于ios - 具有多个 View Controller 的 Swift 3 中的核心数据实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41844850/