0

I have 2 UIViewControllers which contain Tables: A and B.
Tapping a row in the table in A segues to B.

At the bottom of each view of A and B, I have a ContainerView which points to the same UIViewController say Z. I use Z to show banner-ads. The issue I have is each time my view changes (from A to B or B to C), the UIViewController Z gets re-instantiated as it should. But this is what I don't want. I want to use the same instance of the ContainerView everywhere. I did keep my ad-banners static so they are the same everywhere, but still managing orientation-changes and banner-views is getting messy. Also it makes the ad-banner disappear and re-appear when I switch my view, as the container-view instance is switched.

Is there a way that I can keep the same instance of the entire ContainerView in all my UIViewControllers A and B and anyother viewcontrollers I add ?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
rgamber
  • 5,749
  • 10
  • 55
  • 99

1 Answers1

1

There are two approaches which will accomplish this task.

First Approach: Realize that it's your A, B, & C view controllers which should be in the container rather than the banner add view controller. Optionally, make a parent view controller with two containers--one for the banner ads, the other for the A, B, & C controllers.

Second Approach: When segueing from A to B to C, simply pass this view controller along. You could extraordinarily simplify this by given them all a single common parent.

class BannerViewController { /* blah */ }

class BannerViewDisplayViewController {
    @IBOutlet var bannerView: UIView!
    var bannerViewController: BannerViewController! {
        didSet {
            bannerView = bannerViewController.view
            bannerViewController.didMoveToParentViewController(self)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        if bannerViewController == nil {
            // instantiate a bannerViewController
        }
    }   

    override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
        if let destination = segue.destinationViewController as? BannerViewDisplayViewController {
            destination. bannerViewController = self. bannerViewController
        }
    }
}
nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • I did think of the first approach, but the way I am using the Navigaitionbar and Toolbar in A,B,C currently will make it a lot harder (the question was oversimplified!). Second approach seems so obvious! I wonder why I did not think of it. Let me implement, and then accept the answer. Many thanks! – rgamber Jul 12 '15 at 19:34
  • Thinking about it, you probably don't really need to pass the view controller between the A, B, & C. You instead need to pass the BannerViewController's view between the view controllers (it is still controlled by the `BannerViewController`. – nhgrif Jul 12 '15 at 20:01
  • But then `BannerViewController` would be instantiated for every view, right? – rgamber Jul 12 '15 at 20:11
  • I tried instantiating the `BannerViewController` using the StoryBoardId, and then assigned its view to the container view in `A`, `B`, `C`. There is no error, but the view does not show either. `viewDidLoad()` is called, but `viewWillAppear()`,etc are never called. If you happen to be around, please update your code snippet above to reflect your suggestion! Thanks. – rgamber Jul 12 '15 at 20:57
  • View will appear is *never* called or called just once? – nhgrif Jul 12 '15 at 21:28
  • Nevermind, I'm sorry. I was not calling the `bannerViewController.didMoveToParentViewController(parentVC)`. Now it works as desired. Thanks a lot for the suggestion! I had spent quite a bit of time on this!! With this, I can literally use this container-view instance anywhere in my app, pretty cool! – rgamber Jul 12 '15 at 21:34
  • Are you passing the view controller? Or the view? (Do I need to update the code snippet in answer?) – nhgrif Jul 12 '15 at 21:38
  • sorry, I am just passing the view and not the VC. I removed the storyboard segues going to the container view, and used the storyboard-id to instantiate it programatically. It'd be great if you can update your answer so that someone else who crosses by can use it! – rgamber Jul 12 '15 at 21:44