0

Before I get started I am aware this question has been asked many times before, however all of them refer to xcode5/objective-C, not swift. I am only new to app development so I haven't been able to understand the objective-c and use it in swift.

I have got an adBannerView working on my first view controller, however how do I then take this banner and use it across my other 2 view controllers? Do I use the prepareForSegue function (and if so, how)?

My code for the adBannerView I currently have (from here)

//...
import iAd

class ViewController: UIViewController, ADBannerViewDelegate {

//link adBanner
@IBOutlet var adBannerView: ADBannerView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.canDisplayBannerAds = true
    self.adBannerView.delegate = self
    self.adBannerView.hidden = true
}

func bannerViewWillLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewWillLoadAd")
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    NSLog("bannerViewDidLoadAd")
    self.adBannerView.hidden = false
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    NSLog("bannerViewDidLoadAd")
    //optional resume paused game code

}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    NSLog("bannerViewActionShouldBegin")
    //optional pause game code

    return true
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    NSLog("bannerView")
}

//...

Thanks :)

Community
  • 1
  • 1
Luke Martin
  • 395
  • 4
  • 14

1 Answers1

1

When I have faced this problem my solution was to create a ParentViewController, and the others view controllers inheritate from him. In the parent view controller I create an outlet for a view which will contain iAdView, and in the .xib file of each view controller I link the iAdView container view to the created outlet. After that I create a singleton which has all the iAdView functionality and a property which is the iAdView. In the viewDidAppear of the parent I ask the singleton for the iAdView and add it as a subview of the iAd view container. Doing this that way in the view controllers you are not going to see any code of the iAdView, because all will be in the ParentViewController and in the singleton (lets call it iAdManager). Hope it helps.

diegomen
  • 1,804
  • 1
  • 23
  • 37