ios - 将 Core Data 对象从 tableViewController 传递到 TabBarController

标签 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?
}

并尝试在 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/

相关文章:

ios - Swift 的 Facebook SDK 文档是否过时?对应的SDK版本是多少?

iOS - 在输入过程中验证用户 ip 地址

swift - Swift 中的公共(public)扩展 : Float vs Float. 类型

swift - 编译器不允许我比较类型

iphone - 用 View 覆盖标签栏

ios - 我想让我的 UIImage 使用 resizableImageWithCapInsets 在 iPhone 和 iPad 上看起来一样

android - 转码后的 MPEG-4 视频无法在 Android 的 native 应用程序上播放

ios - 使用 UITableViewCellDeleteConfirmationControl 知道当前单元格的 subview 是什么

ios - 无法使用 self.tabBarController?.selectedIndex 更改选项卡

ios - iPad 上的表格 View 覆盖选项卡栏