将Core Data对象从tableViewController传递给TabBarController

时间:2017-07-20 17:22:15

标签: ios swift uitabbarcontroller

我正在尝试将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?
}

并尝试在子视图viewDidLoad

中获取它
override func viewDidLoad() {
    super.viewDidLoad()

    let tbvc = self.tabBarController as! CaseDetail
    casE = tbvc.casE
    print(casE as Any)
}

但我仍然没有,有没有人知道传递数据的其他方法?

感谢您的任何建议和帮助!

1 个答案:

答案 0 :(得分:0)

好的,在阅读Apple文档后,我发现了一个很好的说明

  

每当您在应用程序中传递NSManagedObject引用时,将它们声明为弱引用是有益的。这有助于在NSManagedObject被删除并保留对不存在的对象的悬空引用时保护视图控制器。当属性声明为弱时,删除对象时会自动将其设置为nil。

这样点了,我更改了TabBarController的子类:

class CaseDetail: UITabBarController {

      weak var casE: Case? 
}

现在它完美无缺!

相关问题