![](/img/trans.png)
[英]Passing an object from one viewcontroller to another viewcontroller got nil in swift
[英]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!
您將獲得catID
和firstname
let catID = item.catID
let firstname = item.fullname.firstname
自己弄清楚數據結構很容易:鍵入item.
並查看Xcode的建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.