簡體   English   中英

快速將數組值從一個ViewController傳遞到另一個ViewController

[英]Swift passing array values from one viewcontroller to another

在我的場景中,每當我嘗試單擊tableView自定義cell時,我都將JSON數據加載到tableView ,我將獲取單元格indexPath.row數據。 現在,我試圖將特定的單元格array數據傳遞給另一個viewcontroller 在這里,我需要verify以下代碼是否正確,以及如何從數組中獲取所有values並在第二個viewcontroller labels上顯示?

下面的代碼將數組值一個傳遞給另一個viewcontroller

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    print(self.tableArray[indexPath.row])
    tableView.deselectRow(at: indexPath, animated: true)

    let smallVC = storyboard!.instantiateViewController(withIdentifier: "popupview")  as! PopupViewController
    let item = tableArray[indexPath.row]
    smallVC.array = [item]
    present(smallVC, animated: true, completion: nil)
}

第二個ViewController

var array = [Datum]() //Datum decodable root

override func viewDidLoad() {
    super.viewDidLoad()
    print(“DATA: \(array)") //here, i need to get all values and assign label.
}

我的輸出

數據:[parseJSON.Datum(catID:“ 3”,catName:“ Cars”,catParentid:“ 2”,名稱:“ jhon”,年份:“ 3000”,全名:parseJSON.Fullname(名字:“ jio”,姓氏:“ jack”),地址:parseJSON.Address(城市:“ sanfrancisco”,狀態:“加利福尼亞”),船舶:parseJSON.Ship(船長:“ mojo”,時間:“ 12.30.01”)))]

首先, item是數組的一個對象。 數組是沒有意義的。

smallVC.item = item

在第二個ViewController中

var item : Datum!

您將獲得catIDfirstname

let catID = item.catID
let firstname = item.fullname.firstname

自己弄清楚數據結構很容易:鍵入item. 並查看Xcode的建議。

暫無
暫無

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

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