Reputation: 59
I tried to pass array to next view controller from my table view..
here is my code this far.
@IBAction func done(_ sender: Any) {
let view = self.storyboard?.instantiateViewController(withIdentifier: "pilihtabel") as! labelpilih
for index in 0 ... (tabelpilihan.indexPathsForSelectedRows?.count)! - 1{
let selectedrow = tabelpilihan.indexPathsForSelectedRows?[index].row
print(pilihan[(tabelpilihan.indexPathsForSelectedRows?[index].row)!])
view.pilih = pilihan[selectedrow!]
}
self.navigationController?.pushViewController(view, animated: true)
}
it works , but only pass the last row i picked.
Upvotes: 1
Views: 799
Reputation: 215
Another Way to pass it
@IBAction func done(_ sender: Any) {
let view = self.storyboard?.instantiateViewController(withIdentifier: "pilihtabel") as! labelpilih
let arrPassing = NSMutableArray()
for index in 0 ... (tabelpilihan.indexPathsForSelectedRows?.count)! - 1{
let selectedrow = tabelpilihan.indexPathsForSelectedRows?[index].row
arrPassing.addObject(pilihan[selectedrow!])
print(pilihan[(tabelpilihan.indexPathsForSelectedRows?[index].row)!])
}
view.pilih = arrPassing
self.navigationController?.pushViewController(view, animated: true)
Upvotes: 3
Reputation: 2269
In the line view.pilih = pilihan[selectedrow!]
you are assigning the value pilihan[selectedrow!
to your next view's pilih
variable.
But the pilih
is not of array type so at the end of the loop the last value is overwritten to it and hence it only contains the last value.
To solve this you should make it array type and should append the values like
view.pilih.append(pilihan[selectedrow!])
Upvotes: 2