【问题标题】:Remove iAds from SKScene从 SKScene 中删除 iAd
【发布时间】:2015-10-17 01:59:10
【问题描述】:

我目前正在尝试像这样在我的 SKScene 中实现 IAP Swift Sprite Kit In App Purchase 但我在弄清楚如何从我的 skscene 中将 self.canDisplayBannerAds 设置为 false 时遇到问题,我使用的代码什么也没做。任何帮助将不胜感激。

 func removeAds() {

        let viewController: GameViewController = GameViewController()

        viewController.canDisplayBannerAds = false
    }

【问题讨论】:

    标签: ios swift sprite-kit


    【解决方案1】:

    您的代码不工作的原因是因为您正在创建一个新的 GameViewController 并在其上设置 canDisplayBannerAds。

    您需要保留对原始 GameViewController 的引用,您应该能够通过场景 SKView 从场景中访问它。

    如果您不想继承您的 SKView,您可以使用以下内容来获取当前的 viewController。

    if let viewController = view.nextResponder as? GameViewController /* or whatever your VC is */ {
        viewController.canDisplayBannerAds = false
    }
    

    如果您的 SKView 是子视图,请将 view.nextResponder 更改为 view.superview.nextResponder

    您也可以使用通知中心向您的GameViewController发送消息

    在您的 GameViewController 中。

    override func viewDidAppear(animated: Bool) {
         super.viewDidAppear(animated)
         NSNotificationCenter.defaultCenter().addObserver(self, selector: "turnOffAds", name: "TurnOffAdsNotification", object: nil)
    }
    
    func turnOffAds() {
         self.canDisplayBannerAds = false
    }
    

    在您的 SKScene 中的某处:

    NSNotificationCenter.defaultCenter().postNotificationName("TurnOffAdsNotification", object: nil)
    

    【讨论】:

    • 我明白了。我怎么能这样做呢?我想使用 self.view?.canDisplayBannerAds = false 但这不起作用,因为显然 'SKView 类型的值没有成员 canDisplayBannerAds'
    • 发布了一些代码,试一试,尚未测试,但应该可以。
    • 确定你的 vc 设置为 GameViewController?什么不起作用?
    • 是的,它是 GameViewController。我没有收到任何错误,只是没有删除广告
    • 我不知道你的视图层次是什么,所以很难知道给你什么。我唯一的其他建议是使用 NSNotificationCenter 并在您想要删除这些广告时发布通知,并在您的 GameViewController 中有一个监听器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多