Using segue to pass two types of string back

Hi I'm trying to pass two different strings to a previous viewcontroller but it only shows one where I need it to display.

@IBAction func goBack(_ sender: UIButton) {
self.theName = (nameField.text!)
performSegue(withIdentifier: "passName", sender: self)
self.resLab = youRes.text!
performSegue(withIdentifier: "passPer", sender: self)
}

@IBOutlet weak var nameField: UITextField!


override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "passName" {
    if let vc = segue.destination as? ViewController {
    vc.nameLabel = self.theName
if segue.identifier == "passPer" {
    if let vc = segue.destination as? ViewController {
    vc.perLabel = self.resLab
        }
    }

}
}

}

//controller I want to pass it back to

override func viewDidLoad() {
super.viewDidLoad()
print(perLabel)
labelN.text! = "(nameLabel) (perLabel)"
}

If I understood well you want to pass BACK two string from a new VC (vc2) to the parent (vc).

First, you do not need two segue, an action will create a call to one segue at a time, but you can assign more variables in the same call:

if segue.identifier == "passName" {
if let vc = segue.destination as? ViewController {
vc.nameLabel = self.theName
vc.perLabel = self.resLab
}
}

Secondly you can use the Unwind (to be placed in vc, it will get the data from vcb before it is dismissed):

@IBAction func unwindToThisView(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? vcb {
nameLabel = vcb.theName
perLabel = vcb.resLab
}
}

Hope this helps!

Terms of Service

Privacy Policy

Cookie Policy