【问题标题】:Passing segue data through a Google AdMob interstitial delegate method通过 Google AdMob 插页式委托​​方法传递 segue 数据
【发布时间】:2016-08-31 18:57:13
【问题描述】:

我有一个应用程序,它有四个按钮,它们都将数据传递给模态视图控制器。这是通过为 4 个按钮分配标签并在 prepareForSegue 中添加一些条件来实现的

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if segue.identifier == "modalSegue" {
        let secondViewController = segue.destinationViewController as! ModalViewController
        if sender?.tag == 1 {
            secondViewController.title = "Lawn Mowing"
        }
        else if sender?.tag == 2 {
            secondViewController.title = "Landscaping"
        }
        else if sender?.tag == 3 {
            secondViewController.title = "Hardscaping"
        }
        else if sender?.tag == 4 {
            secondViewController.title = "Tree Removal"
        }
    }
}

这是我的按钮:

@IBAction func servicesButton(sender: UIButton) {
    adCounter += 1
    if adCounter == 4 {
        adCounter = 0
        if (interstitial.isReady) {
            interstitial.presentFromRootViewController(self)
            interstitial = createAd()
        }
        else {
            self.performSegueWithIdentifier("modalSegue", sender: sender)
        }
    }
    else {
        self.performSegueWithIdentifier("modalSegue", sender: sender)
    }
}

完美运行。

当我介绍 AdMob 插页式广告时,问题就来了

每点击 4 个按钮(在 servicesButton 上),我就会调用一次插页式广告。当插页式广告关闭时(通过 interstitialDidDismissScreen),我对模态视图控制器执行 segue:

func interstitialDidDismissScreen(ad: GADInterstitial!) {
    self.performSegueWithIdentifier("modalSegue", sender: nil)
}

但是没有数据通过!! :( 模态只是在没有 secondViewController 标题的情况下加载。我相信这是因为没有发送者,它给出按钮标签并传递数据......对吗?我还尝试了插页式委托​​方法中的 sender: self ,但没有做任何事情。

用户关闭插页式广告后,如何继续将此信息传递给模态视图控制器?还是有更好的方法来处理这个问题?我需要在 segue 之前加载插页式广告,因为意外点击的可能性要小得多。

谢谢。如果您需要进一步的说明或问题,请告诉我。

【问题讨论】:

    标签: ios swift admob segue interstitial


    【解决方案1】:

    一种解决方案是在展示插页式广告时清除 IBAction 中的计数,然后在取消插页式广告时,如果您有 IBOutlets,您可以使用作为发件人按下的按钮调用 servicesButton 函数。

    func interstitialDidDismissScreen(ad: GADInterstitial!) {
        self.servicesButton(buttonThatWasPressed) 
    }
    

    这会回忆起你所有的逻辑。

    另一种方法是为新视图标题设置一个类级变量,并设置当按下按钮时,无论您从哪里调用 presentSegue,在 prepareForSegue 中,您仍然可以访问新标题的内容是。

    【讨论】:

    • 在这里感谢您的帮助。我有点卡在 buttonThatWasPressed 上。如何将此信息传递给 interstitialDidDismissScreen 方法?
    • 如果您在代码中或作为 IBOutlet 有按钮,那么这就是您作为 sender 参数传递的按钮
    • 我在上面的原始帖子中没有提到的一件事:我将所有 4 个按钮都链接到同一个 touchUpInside 事件 (servicesButton)。我在故事板中做到了这一点。然后我只为每个设置一个标签。我认为这将是更简洁的代码,因为所有 4 个按钮都调用相同的逻辑。
    • 好的,那么您需要采用我的第二种方法,并在您按下按钮时将新标题设置为您可以在 prepareForSegue 中签入的类变量
    • 我实际上已经非常接近您的第一个建议了! :) 我添加了一个 IBOutlet 并将其作为发件人:self.servicesButton(lawnmowingOutlet) - 但我该如何处理其他 3 个? Switch 语句?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多