You are on the right track to setup the delegate.
In Storyboard:
- create a Segue of type
Embed Segue
from theContainerView
to the ViewController to embed. - Select the segue and set a
Identifier
.
In Code, in prepareForSegue()
:
- Set the
myContainerView
propert -
Set the delegate
class ViewController: UITableViewController, ContainerViewDelegate { var myContainerView:ContainerView? // we do initialize it in prepareForSegue() var id: String! override func viewDidLoad() { super.viewDidLoad() id = "123" // myContainerView.delegate = self //myContainerView is nil here. We set it in prepareForSegue } func initialize() -> String { return id } let embedSegueID = "the identifier you set to your embed segue in Storyboard" override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if (segue.identifier == embedSegueID) { let dvc = segue.destination as! ContainerView // Now you have a pointer to EmbeddedViewController. // You can save the reference to it, or pass data to it. myContainerView = dvc myContainerView.delegate = self } print(segue.identifier) } }
If you want to pass data from your ViewController to AnotherViewController which is embedded in your destinationViewController (how I understand your question), I would:
- pass it to destinationViewController as in the example above (prepareForSegue)
- than pass it to AnotherViewController in
viewWillAppear()
from within destinationViewController.
In this case, how I understand your usecase, you do not need the delegate and can remove all delegate related code (initialize
, the ContainerViewDelegate
protocol, the property var delegate: ContainerViewDelegate!
and all calls to it.
2
solved Share Data Between View Controller and Container View