1

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.

AlbertWu
  • 59
  • 1
  • 8

2 Answers2

3

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)
Vijay Ladva
  • 215
  • 4
  • 10
  • what is the different between NSMutableArray and Array or NSArray? – AlbertWu Dec 10 '16 at 05:00
  • @AlbertWu In objective C NSArray is used to define an array which cannot be altered/changed later while NSMutableArray is used to define an array which can be altered/changed later on – Aakash Dec 10 '16 at 05:03
  • @AlbertWu since in swift constants are defined used let and variables by var hence Array type variable will be of mutable type if it is defined using var and immutable if define using let – Aakash Dec 10 '16 at 05:05
2

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!])

Aakash
  • 2,239
  • 15
  • 23
  • thank you, it's work perfectly. Can you explain a little bit about **append** ? how can i miss that – AlbertWu Dec 10 '16 at 04:53
  • @AlbertWu you needed to pass some values from pilihan (and not the single value) hence the next view's pilih variable must be an array type so you can to add those values in the pilih variable, therefore "append" is used to add those values one by one. – Aakash Dec 10 '16 at 04:58
  • when i tried to change my pilih in next view's to Array type. it shows "",array1,array2. this is how i declare pilih to array type 'var pilih : Array = [""]' – AlbertWu Dec 10 '16 at 04:58
  • you should change that line to var pilih = Array(), this will initialise the variable without any element. – Aakash Dec 10 '16 at 05:00
  • okay thank you so much. I am really a newbie, and must learn a lot . i will keep in mind about that "append" btw. – AlbertWu Dec 10 '16 at 05:12
  • hi @Aakash wht's the pilihan in this case? – deltami Sep 09 '17 at 06:29
  • @deltami it is a randomly named array variable – Aakash Sep 09 '17 at 07:03
  • @Aakash thanks mate but its not working in my case :( . hey will you try to solve my question? – deltami Sep 09 '17 at 07:10
  • @deltami sure what is it? – Aakash Sep 09 '17 at 08:24
  • @Aakash hey sorry for replying late see this question https://stackoverflow.com/questions/45892379/how-to-apply-filter-based-on-multiple-selected-rows-in-uitableview-using-swift-3 problem is same want to pass all the selected values to the next view controller on button click but scenario is different. – deltami Sep 09 '17 at 11:09