在ViewControllers之间传递NSManagedObject

时间:2016-11-22 10:59:31

标签: ios swift core-data swift3 nsmanagedobject

我想在NSManagedObject之间传递ViewControllers。 为此我使用全局变量。

在我的第一个viewcontroller中,在顶部(作为全局变量):

var choosenItem : NSManagedObject? = nil

我构建了我的表视图,我有这个:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let choosenOne = listOfLands[indexPath.row]
    let choosenItem = listOfLands[indexPath.row]


    let viewController = storyboard?.instantiateViewController(withIdentifier: "ViewController2")
    self.navigationController?.pushViewController(viewController!, animated: true)

    print(choosenOne.objectID)
    print(choosenItem.objectID)

}

当我打印所选择的对象时,我得到了它们。所以我认为在ViewController中一切正常。

在ViewController2中我有:

   override func viewDidLoad() {

 print(thatLand?.objectID) //result is nil

}

现在我不确定为什么我会在第二个ViewController中获得nil。 有没有办法像这样传递NSManagedObject

1 个答案:

答案 0 :(得分:1)

您需要将choosenItem分配给thatLand的{​​{1}}属性,同时您需要明确地将ViewController2转换为UIViewController。< / p>

ViewController2
相关问题