segue并将数据从viewcontroller传输到viewcontroller到另一个viewcontroller

时间:2016-11-02 07:12:48

标签: ios swift3 segue

抱歉混淆标题,但这是我的问题。我从" superVC"做了一个segue。 (collectionviewcell)to" childVC"并且运作良好。然后我想再次从那个" childVC"到" secondChildVC" (这是所有数据来自superVC)。可能吗?因为我在执行segue时总是得到一个零值。

它是这样的:SuperVC - > ChildVC - > secondChildVC

这是superVC segue:

var destination = [DestinationData]() 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DestinationDetailVC"{
        let detailVC = segue.destination as? DestinationDetailVC
        if let data = sender as? DestinationData{
            detailVC?.destinationDetail = data
        }
    }
}

这是第二个vc segue

var destinationDetail : DestinationData?
@IBAction func goToMapOnPressed(_ sender: Any) {
    let detail = destinationDetail
    self.performSegue(withIdentifier: "DestinationMapVC", sender: detail )
    print(detail)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "DestinationMapVC"{
        let detailVC = segue.destination as? DestinationMapVC
        if let data = sender as? DestinationData{
            detailVC?.destinationMapDetail = data
        }
    }
}

由于

1 个答案:

答案 0 :(得分:0)

我认为你要将DestinationData的数组发送到第一个segue,但是在第一个segue中你放了条件,所以如果数据是DestinationData种类,那么数据将是发送到nextVC,但实际上你发送的是DestinationData对象的数组,因此if条件失败,因此数据不会传递给childVC

这就是为什么你可能在secondChildVC中获得nil,因为destinationDetail childVC中没有数据。

因此,您可以修改条件以检查数组,因为destination包含数组类型的数据。或者从prepareSegue方法中删除条件。

代码:

//Write this code in to get that clicked object then pass that object in sender
let destTemp = sender[your selected index]

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "DestinationDetailVC"{
            let detailVC = segue.destination as? DestinationDetailVC
            if let data = sender as? DestinationData{
                detailVC?.destinationDetail = data
            }
        }
    }

希望它有所帮助。

快乐的编码......