I have an app that has four buttons that all pass data to a modal view controller. This is accomplished by assigning tags to the 4 buttons, and adding some conditionals inside 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"
}
}
}
Here is my button:
@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)
}
}
Works perfect.
The problem comes when I introduce AdMob Interstitials
Every 4th button tap (on servicesButton), I call an Interstitial. When the interstitial is closed (via interstitialDidDismissScreen), I perform the segue to the modal view controller:
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.performSegueWithIdentifier("modalSegue", sender: nil)
}
But no data gets passed!! :( The modal just loads up without the secondViewController title. I believe it is because there is no sender, which gives the button tags and passes the data... Correct? I also tried sender: self in the interstitial delegate method, but that did not do anything.
How can I continue to pass this information to the modal view controller after the user dismisses the interstitial? Or is there an overall better way to handle this? i NEED the interstitial to load up BEFORE the segue, as there will be much less of a chance of accidental taps.
Thanks. If you need further clarification or questions please let me know.
One solution would be to clear your count in your IBAction when you present the interstitial, and then when the interstitial is dismissed you can call your servicesButton function with the button that was pressed as the sender if you have IBOutlets for them.
func interstitialDidDismissScreen(ad: GADInterstitial!) {
self.servicesButton(buttonThatWasPressed)
}
This will then recall all your logic.
Another way would be to set a class-level variable for the new views title and set that when the button is pressed, then no matter where you call presentSegue from, in prepareForSegue you will still have access to what the new title will be.