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
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.
© 2024 OneMinuteCode. All rights reserved.