0

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
    }



}

1 Answers1

0

If the second view controller is a UISplitViewController for example you should be able to append the text of the button to the label, by doing something in the lines of:

@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!

    }
    }

If you really have a separated view controller it will probably lose the data once you go back to the first VC. In that case, I suggest you save the current text of the label on viewWillDisappear of the 2nd VC and then retrieve this on the prepareforsegue function on the first VC. More info here: Swift: How to store user preferences?