0

Im passing data through 3 VCs, so in the end I want to achieve sending data from the third VC to the first. I send Data from V2 to V3 with a segue and then send it back from V3 to V2 by delegate. Im then trying to send it from V2 to V1 through a segue but I cant seem to collect the data (sent back from V3) in V2 to then send to V1. The data from V3 doesn't show up in V1, but the code still runs.

Can anyone help?

heres my code from V2 and V3:

V2:

import UIKit

class SecondViewController: UIViewController, thirdDelegate {
var GetBack: String?
var SendForward = [String]()
var Datacollect = [String]()
var Collect = [String]()
let ct = "Conner@2"
let new = "All@2"

@IBOutlet var Hinput: UITextField!

@IBOutlet var Ninput: UITextField!

@IBAction func MAP(_ sender: Any) {
    if Hinput.text != ""{
        performSegue(withIdentifier: "SegueSearch", sender: self)}
    }

@IBAction func Info(_ sender: Any) {
    performSegue(withIdentifier: "SegueInfo", sender: self)
}





func DataToPass(ArrayName: [String]) { //function from delegate
   Datacollect = ArrayName
    print(ArrayName)
}



override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == "SegueSearch"{
      let thirdController = segue.destination as! ThirdViewController
        SendForward.append(Hinput.text!)
        SendForward.append(ct)
        thirdController.height = SendForward
        thirdController.delegate = self
    } else if segue.identifier == "SegueInfo" {
        let firstController = segue.destination as! ViewController
        if Datacollect.count != 0{
            Collect.append(Datacollect[1])
            Collect.append(Datacollect[0])}
        Collect.append(Ninput.text!)
        Collect.append(new)
        firstController.AllData = Collect

            }
    }

override func viewDidLoad() {
    super.viewDidLoad()
    print("check",Datacollect)
    // Do any additional setup after loading the view.
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.

     }

  }

Is this the part that isn't working?

func DataToPass(ArrayName: [String]) {
   Datacollect = ArrayName
    print(ArrayName)
}

V3:

    import UIKit

    protocol thirdDelegate{
func DataToPass (ArrayName: [String])
}

 class ThirdViewController: UIViewController {
var height = [String]()
var SendBack = [String]()
let ko = "Keith@3"


var delegate: thirdDelegate! = nil


@IBOutlet var Houtput: UILabel!

@IBAction func Home(_ sender: Any) {
    let StrH = String(height[0])
    SendBack.append(ko)
    SendBack.append(StrH!)
  delegate.DataToPass(ArrayName: SendBack)
  }



override func viewDidLoad() {
    Houtput.text = height[0]
    super.viewDidLoad()

    print(height)

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
     }


}
Dan.code
  • 431
  • 2
  • 14
  • Please add code for the ThirdViewController – xmhafiz Jun 27 '17 at 22:32
  • Added ThirdViewController – Dan.code Jun 27 '17 at 22:41
  • what is the output when you `print(ArrayName)` in `DataToPass` method? – xmhafiz Jun 27 '17 at 22:57
  • Print(ArrayName) prints out `["Keith@3", "5.4"]` where the 5.4 is the Hinput from the text field. And from `print("check",Datacollect)` the print out is `check [ ]` – Dan.code Jun 27 '17 at 23:05
  • your code is correct, the passed data will go into the `DataToPass`, not view didload. `viewDidLoad` only will be called once (at the first time you see it) – xmhafiz Jun 27 '17 at 23:42
  • How would I then take the data from DataToPass and then send it using a segue to my first ViewController? – Dan.code Jun 27 '17 at 23:45
  • use unwind as discussed [here](https://stackoverflow.com/questions/35313747/passing-data-with-unwind-segue) or create another delegate from v2 to v1, and call that delegate in the `DataToPass` – xmhafiz Jun 28 '17 at 00:27

0 Answers0