xcode11 segue unwind function prepare not executed

105 Views Asked by At

I have two view controllers A,B in controller scenes. I can create a segue from A -> B. I also create an unwind segue from B -> A with a prepare function. This function is being executed but the value is not being passed to passedDataString is not being passed back.

import UIKit

class homeViewController: UIViewController , UIAlertViewDelegate {
    @IBOutlet weak var returnStatusLbl: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
         
        // Do any additional setup after loading the view.
    }
    var passedDataString  = "QR Return not set"


    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
        returnStatusLbl.text = passedDataString
   }
    
    @IBAction func QRtoHome( _ sender: UIStoryboardSegue) {}
    
}
import UIKit

class QRViewController: UIViewController {

        @IBOutlet weak var wherefromLbl: UILabel!
        override func viewDidLoad() {
            super.viewDidLoad()
    }
    
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(true)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let destVC = segue.destination as! homeViewController
        destVC.passedDataString = "From Done"
    }    
}
2

There are 2 best solutions below

0
claude31 On

When you return by unwind, viewWillAppear is not called.

You could use an observer:

   var passedDataString : String = "QR Return not set" {
     didSet {
        returnStatusLbl.text = passedDataString
     }
   }
0
odbdux On

I found this works:

    @IBAction func QRtoHome( _ sender: UIStoryboardSegue) {
        returnStatusLbl.text = passedDataString
    }