Home > Blockchain >  Action on the previous view controller after a dismiss
Action on the previous view controller after a dismiss

Time:10-06

I have two view controllers. By pressing button1, I change from VS_1 to VS_2. By pressing button2, I go to the view controller_1. I need button1 to change its color to red at this moment. How to do it?

class VC_1: UIViewController{

..........
var button1 = UIButton()
button1.backgroundColor = .green

@IBAction func goToVC_2(_ sender: Any) {

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vC2 = storyBoard.instantiateViewController(withIdentifier: "VC2") as! VC_2

    self.present(vC2, animated: true, completion: nil)
}

}

class VC_2: UIViewController{

..........
var button2 = UIButton()
button2.backgroundColor = .green

@IBAction func goToVC_1(_ sender: Any) {

    self.dismiss(animated: true, completion: nil)
}

}

CodePudding user response:

You can do it by Callback, Delegate etc.

By callback:

@IBAction func goToVC_2(_ sender: Any) {

    let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let vC2 = storyBoard.instantiateViewController(withIdentifier: "VC2") as! VC_2

    vC2.didTapButton = {
        // change color here 
    }
    self.present(vC2, animated: true, completion: nil)
}


var didTapButton: () -> Void = { }

@IBAction func goToVC_1(_ sender: Any) {
    didTapButton()
    self.dismiss(animated: true, completion: nil)
}
  • Related