问题在于,每次加载视图时,您都会创建一个新的实例
ADBannerView。我们需要做的是
ADBannerView在我们的视图中创建一次
AppDelegate.swift,然后
ADBannerView在我们希望使用iAd横幅的任何视图上展示该视图。这也称为“
共享iAd标语”。在这个例子中,我创建了一个
ADBannerView在我
AppDelegate.swift,然后把它添加到我
ViewController.swift的观点。
AppDelegate.swift
import UIKitimport iAd // import iAd@UIApplicationMainclass AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate { // Include the delegate for our banner var window: UIWindow? var adBannerView = ADBannerView() // Create our one ADBannerView func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Set delegate and hide banner initially adBannerView.delegate = self adBannerView.hidden = true return true } func bannerViewDidLoadAd(banner: ADBannerView!) { print("bannerViewDidLoadAd") adBannerView.hidden = false } func bannerViewActionDidFinish(banner: ADBannerView!) { print("bannerViewActionDidFinish") } func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { print("didFailToReceiveAdWithError: (error)") adBannerView.hidden = true }
ViewController.swift
import UIKitclass ViewController: UIViewController { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // Create reference to our app delegate override func viewWillAppear(animated: Bool) { // Position appDelegate.adBannerView.center = CGPoint(x: view.frame.midX, y: view.frame.height - appDelegate.adBannerView.frame.height / 2) // Add to view view.addSubview(appDelegate.adBannerView) }
不要忘记从
viewWillDisappear(animated:Bool)以前添加的函数中删除代码。如果您单击横幅,然后将其关闭,则将调用此函数,并且从视图中删除横幅并将横幅委托设置为nil太早会导致问题。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)