Access container view child properties swift

后端 未结 4 1475
情书的邮戳
情书的邮戳 2021-02-12 04:20

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.

4条回答
  •  -上瘾入骨i
    2021-02-12 04:38

    Step by step:

    1. Name the segue between your view controller and container view controller.
    2. Add a property to your view controller which will contain the container view controller.
    3. In your view controller implement a method prepareForSegue(_:sender:).
    4. In the method check if segue.identifier equals the identifier you specified in step 1.
    5. If true, then save the segue.destinationViewController to your property from step 2.
    6. Now you have the container view controller stored in your property so you can do customization from your class. You should have the view controller stored in 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
        }
    }
    

提交回复
热议问题