What I want to achieve:
User presses the button in the ViewController then, the color of the button placed in the container view should change its color to red.
How can I get access of the button placed in the container view, from the ViewController?
Step by step:
prepareForSegue(_:sender:)
.segue.identifier
equals the identifier you specified in step 1.segue.destinationViewController
to your property from step 2.viewDidLoad()
method already.Example:
var containerViewController: YourContainerViewControllerClass? let containerSegueName = "testSegue" override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { if segue.identifier == containerSegueName { containerViewController = segue.destinationViewController as? YourContainerViewControllerClass } }
I recommend not to rely on segue.identifier
, but rather test for destination
type directly:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { super.prepare(for: segue, sender: sender) if let vc = segue.destination as? YourViewController { vc.someVariable = true } }
This way you avoid mistakes with a misspelled segue name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With