簡體   English   中英

查找數據並將數據從ViewController傳輸到另一個ViewController

[英]segue and transfer data from viewcontroller to viewcontroller to another viewcontroller

抱歉,標題令人困惑,但這是我的問題。 我做了一個從“ superVC”(collectionviewcell)到“ childVC”的工作,它的工作很好。 然后我想再次從那個“ childVC”到“ secondChildVC”(這是所有數據都來自superVC)。 可能嗎? 因為執行該測試時我總是得到nil值。

它是這樣的: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
        }
    }
}

謝謝

我認為您正在將DestinationData數組發送到第一個segue,但是在第一個segue中已經設置了條件,因此,如果數據將是DestinationData類類,則數據將被發送到nextVC,但是實際上您正在發送數組的DestinationData對象,因此if條件失敗,因此數據不會傳遞給childVC

這就是為什么你會在越來越零secondChildVC ,因為在沒有數據destinationDetailchildVC

因此,您可以修改條件以檢查數組,因為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
            }
        }
    }

希望能幫助到你。

快樂的編碼...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM