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 ?
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
}
}
}