使用 isHidden = true 隐藏横幅广告

Hide Banner Ad with isHidden = true

我目前正在使用我的 GameViewController 中的以下代码从 admob 调用横幅广告。

class GameViewController: UIViewController {
var bannerView: GADBannerView!
}

override func viewDidLoad() {
    super.viewDidLoad()
    bannerView = GADBannerView(adSize: kGADAdSizeBanner)
    addBannerViewToView(bannerView)
    bannerView.adUnitID = "ca-app-pub-XXXXXXXXX"  // Default Google Test Ad Address
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    bannerView.isHidden = false // <----- This works, but doesn't work in GameScene.swift

这是从上面的代码(也在 GameViewController 中)调用的函数来显示广告。

func addBannerViewToView(_ bannerView: GADBannerView) {
   bannerView.translatesAutoresizingMaskIntoConstraints = false
   view.addSubview(bannerView)
   view.addConstraints(
     [NSLayoutConstraint(item: bannerView,
                         attribute: .bottom,
                         relatedBy: .equal,
                         toItem: bottomLayoutGuide,
                         attribute: .top,
                         multiplier: 1,
                         constant: 0),
      NSLayoutConstraint(item: bannerView,
                         attribute: .centerX,
                         relatedBy: .equal,
                         toItem: view,
                         attribute: .centerX,
                         multiplier: 1,
                         constant: 0)
     ])
   }

此代码有效并显示了测试广告。但是,在 GameScene.swift 中,当我按下按钮时,我 运行 此代码会尝试隐藏广告。

GameViewController().bannerView.isHidden = true

这会导致应用程序崩溃 "Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value"。如果我更改它,它不会崩溃,但广告不会隐藏。

GameViewController().bannerView?.isHidden = true

然而在 GameViewController 中,如果我简单地将 isHidden 的值更改为 true,那么它就可以工作了。

bannerView.isHidden = true

当我按下按钮时,是否能够从 GameScene.swift 中隐藏横幅广告?

您正在创建 GameViewController 的新实例并将其切换为 bannerViewisHidden 属性。您应该切换您正在显示的 GameViewController 实例的 bannerViewisHidden 属性。

如果有人想知道,这是我找到的隐藏横幅的解决方案:

"Handle all the banner stuff in the GameViewController.swift file. Inside there, assign a tag value for the bannerView variable. Then when the scene loads, load the bannerView using the tag id, then set its .isHidden property to true"

GameViewController.swift

bannerView.tag = 100

GameScene.swift

// hide banner
let bannerView = self.view?.viewWithTag(100) as! GADBannerView?
bannerView?.isHidden = true

// show banner
let bannerView = self.view?.viewWithTag(100) as! GADBannerView?
bannerView?.isHidden = false

当您调用 GameViewController() 时,您正在创建 GameViewController 的新实例。

GameViewController().bannerView 上,您正在从这个新实例访问 bannerView,并且由于它是一个未在此实例上设置的可选值,因此当您尝试访问它时 - 将导致崩溃。

您需要访问主 GameViewController,您在其中设置了 bannerView

有两种简单的方法可以从其他 class 中调用 GameViewController 函数:通过 delegate 或创建 class 实例 static

//   DELEGATE
//
// You have access only to the functions you declared inside the procotol. 
// Only the classes that conforms to that protocol have access to the functions.
//
// GameViewController.swift

protocol GameView_Delegate
{
    func hide_ads()
}

class GameViewController: UIViewController, GameView_Delegate
{
    override func viewDidLoad()
    {

    //   ... 

    if let view = self.view as! SKView?
    {
        if let scene = SKScene(fileNamed: "GameScene")
        {        
            scene.scaleMode = .aspectFill

            // pass protocol reference
            scene.gv_delegate = self
        }

        view.presentScene(scene)
     }

    // ...

    }

    func hide_ads()
    {
        // hide ads code

        bannerView.isHidden = true
    }
}

// GameScene.swift

class GameScene: SKScene
{
    var gv_delegate : GameView_Delegate!

    func settings()
    {
        // call function hide_ads from GameViewController
        gv_delegate.hide_ads()
    }

    func lost_scene()
    {
        let transition = SKTransition.fade(with: UIColor.white, duration: 1.0)
        let next_scene = LostScene()
        next_scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        next_scene.scaleMode = self.scaleMode
        next_scene.size = self.size

        // before moving to next scene, we must pass the delegate, so we
        // don't  lose the connection with the GameViewController
        next_scene.gv_delegate = gv_delegate
        self.view?.presentScene(next_scene, transition: transition)
    }
}

// LostScene.swift

class LostScene: SKScene
{
    var gv_delegate : GameView_Delegate!

    func settings()
    {
        // call function hide_ads from GameViewController
        gv_delegate.hide_ads()
    }

    func game_scene()
    {
        let transition = SKTransition.fade(with: UIColor.white, duration: 1.0)
        let next_scene = GameScene()
        next_scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        next_scene.scaleMode = self.scaleMode
        next_scene.size = self.size

        // before moving to next scene, we must pass the delegate, so we
        // don't lose the connection with the GameViewController
        next_scene.gv_delegate = gv_delegate
        self.view?.presentScene(next_scene, transition: transition)
    }
}

当你进入一个新场景时,例如LostScene,我们例子中的旧场景GameScene被释放(所有变量清空,所有引用被销毁)和一个新的GameScene实例被创建并显示在屏幕上。在转换到新场景之前,您应该传递引用,这样您就不会失去与 GameViewController 的连接(如我的示例所示)。

// STATIC
//
// the properties and functions are available from everywhere
//
// GameViewController.swift

class GameViewController: UIViewController
{
    static var shared : GameViewController!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        GameViewController.shared = self

        // ...

    }

    func hide_ads()
    {
        // hide ads code
        bannerView.isHidden = true
    }
}


// GameScene.swift

class GameScene: SKScene
{
    func settings()
    {
        // call function hide_ads from GameViewController

        GameViewController.shared.hide_ads()
        // or GameViewController.shared.bannerView.isHidden = true
    }
}