How to Change Parent ViewController Parameters from Child ViewController

Asked 2 years ago, Updated 2 years ago, 89 views

This is Swift's program question.
There is UIButton in View in child ViewController.I would like to change the parameters of the parent ViewController by pressing that button.

We have added a child ViewController as follows:

 // Add child ViweController (content)
self.addChildViewController(content)
// Add view of child ViewController to its view hierarchy
self.view.addSubview(content.view)
// Notify child ViewController that add is complete
content.didMoveToParentViewController(self)

If you press Button in the child View, you want to change the parameters of the parent ViewController (for example, vartest: Integer).I wish I could pass the parameters like Segue, but I don't know how to do it.

Please let me know if you know.

swift uikit

2022-09-30 20:59

1 Answers

A simple method is to retrieve the parent ViewController from the parentViewController.

 func click (sender:UIButton) {
    iflet vc: ParentViewController=self.parentViewController as ?ParentViewController {
        vc.test="hogehoge"
    }
}

The optional Binding syntax allows you to cast as? for type checking, so if the visibility of the instance variable is internal or higher, the above description will safely pass it over.


2022-09-30 20:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.