Zoltan Zoltan - 3 months ago 21x
iOS Question

Delegation from ContainerView to parent ViewController

I have the following setup:

StartViewController has a ContainerView that contains ContainerViewController

I try to find a way to hidden an element in StartViewController after a task is performed in ContainerViewController.

For this I try to use delegation method like this:


class StartViewController: UIViewController, showBannerAdDelegate {

@IBOutlet weak var bannerView: UIView!

override func viewDidLoad() {
bannerView.hidden = false

func bannerAdHidden(status: Bool) {
bannerView.hidden = status



protocol showBannerAdDelegate: class {
func bannerAdHidden(status: Bool)

class ContainerViewController: UIViewController {

weak var delegate: showBannerAdDelegate! = nil

@IBAction func buttonPressed(sender: UIButton) {


If I presented the StartViewController I could do in prepareForSegue

let destination = segue.destinationViewController as! DestinationViewController
destination.delegate = self

But in this case I do not present StartViewController because it is always present.

What code should I add to the View Controller to make it work?

Thank you,


If one of the view controllers is inside a container view then it is loaded with an embed segue, which fires when the containing view controller is first loaded. The prepareForSegue method still gets called, so you can set up a delegate exactly as you've described. I always thought embed segues were a little odd (it's not really a segue, more like loading a child view controller) but that's how it works.