Store segue data on different view controller

My code takes text from a textfield and segues to a separate view controller. The problem is that it can only store 1 segue entry. The new one replace the old every time the button is pressed. How on vc2 can I store every new entry to the segue.

VC

   import UIKit

  class ViewController: UIViewController {

@IBOutlet var txt: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

}
@IBAction func pressButton(_ sender: Any) {
    if txt.text != "" {
  performSegue(withIdentifier: "segue", sender: self)

    }
    }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let secondController = segue.destination as! twoViewController
    secondController.myString = txt.text!

}
}

twoVC

 import UIKit

 class twoViewController: UIViewController {

@IBOutlet var labelSmtih: UILabel!
var myString = String()

override func viewDidLoad() {
    super.viewDidLoad()
    labelSmtih.text = myString
}

}

@timswift. Thanks very much for your question!

I’m not sure i follow you, but I’m guessing that you want to store all of the Strings you send from VC1 within VC2? If that is the case, then perhaps you should create an array inside VC2, that is a class variable, and store the Strings inside the Array, appending them one at a time after you load VC2.

I hope this helps!

All the best!

This topic was automatically closed after 166 days. New replies are no longer allowed.