我试图将 Core Data 对象从 tableViewController 中的列表传递到 TabBarController 以在 childViews 中解析它,我执行 segue 到子类 TabBarController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let objs = controller.fetchedObjects , objs.count > 0 {
let casE = objs[indexPath.row]
performSegue(withIdentifier: "showDetail", sender: casE)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showDetail" {
if let destination = segue.destination as? CaseDetail {
if let casE = sender as? Case {
destination.casE = casE
}
}
}
}
因此将其传递给 tabBarController
class CaseDetail: UITabBarController {
var casE: Case?
}
并尝试在 subview viewDidLoad中获取它
override func viewDidLoad() {
super.viewDidLoad()
let tbvc = self.tabBarController as! CaseDetail
casE = tbvc.casE
print(casE as Any)
}
但我仍然没有得到任何结果,有谁知道传递数据的其他方法吗?
感谢您的建议和帮助!
最佳答案
好吧,在阅读Apple文档后,我发现了一个很好的注释
Whenever you are passing NSManagedObject references in your application, it is beneficial to declare them as weak references. This helps to protect your view controller in the event of the NSManagedObject being deleted and leaving a dangling reference to a non-existent object. When the property is declared as weak it is automatically set to nil when the object is deleted.
所以这很重要,我更改了 TabBarController 子类:
class CaseDetail: UITabBarController {
weak var casE: Case?
}
现在效果完美!
关于ios - 将 Core Data 对象从 tableViewController 传递到 TabBarController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45221331/