Juli IchSelber Juli IchSelber - 2 months ago 7
Swift Question

Buttons disappear when ad loads

I coded an app with some buttons. One of them shows an interstitial Ad by pressing it. When I close the ad all the buttons on the view disappear. How can I fix that?

//Collection Sound
var boomSound = NSURL(fileURLWithPath: Bundle.main.path(forResource: "135936__bradwesson__collectcoin", ofType: "wav")!)
var audioPlayer = AVAudioPlayer()



@IBOutlet var ScoreLabel: UILabel!
@IBOutlet weak var BT6: UIButton!
@IBOutlet weak var BT5: UIButton!
@IBOutlet weak var BT4: UIButton!
@IBOutlet weak var BT3: UIButton!
@IBOutlet weak var BT2: UIButton!
@IBOutlet weak var BT1: UIButton!

var interstitial: GADInterstitial!

//When reaching x Taps
var taps = Int(){
didSet {
if taps == 330 {
print("You have reached 5 taps !!")
}
}
}


override func viewDidLoad() {
super.viewDidLoad()





//load interstitial Ad
interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/2951120388")
let request2 = GADRequest()
interstitial.load(request2)



authPlayer()

let defaults = UserDefaults.standard
if let storedTaps = defaults.object(forKey: "key") as? Int {
self.taps = storedTaps
setLabel(storedTaps)

}

}



override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.


}

//HIDE STATUS BAR
override var prefersStatusBarHidden : Bool {
return true
}


@IBAction func BTN(_ sender: AnyObject) {

sender.setTitleColor(UIColor.red, for: UIControlState())

taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

}

func setLabel(_ taps:Int) {
ScoreLabel.text = "TAPS \(taps)"
}

//GameCenter
func authPlayer(){

let localPlayer = GKLocalPlayer.localPlayer()

localPlayer.authenticateHandler = {
(view, error) in

if view != nil {

self.present(view!, animated: true, completion: nil)

}
else {

print(GKLocalPlayer.localPlayer().isAuthenticated)


}
}
}

//GameCenter
func saveHighscore( _ number : Int){

if GKLocalPlayer.localPlayer().isAuthenticated {

let scoreReporter = GKScore(leaderboardIdentifier: "TAPME")

scoreReporter.value = Int64(number)

let scoreArray : [GKScore] = [scoreReporter]

GKScore.report(scoreArray, withCompletionHandler: nil)

}

}

//GameCenter
func showLeaderboard(){

let viewController = self.view.window?.rootViewController

let gcvc = GKGameCenterViewController()

gcvc.gameCenterDelegate = self

viewController?.present(gcvc, animated: true, completion: nil)

}

//GameCenter
func gameCenterViewControllerDidFinish(_ gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismiss(animated: true, completion: nil)
}

//Reset TapsLabel
@IBAction func reset(_ sender: AnyObject) {

taps = 0
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

//load interstitial Ad
if (interstitial.isReady){

interstitial.present(fromRootViewController: self)
interstitial = createAD()




}


}

//Load GameCenter
@IBAction func CallGC(_ sender: UIButton) {

showLeaderboard()
saveHighscore(taps)
}

//When view is loaded
override func viewDidAppear(_ animated: Bool) {
BT6.isHidden = true
BT5.isHidden = true
BT4.isHidden = true
BT3.isHidden = true
BT2.isHidden = true
//Hiding all but one button when the view controller loads
}

//Random button view
@IBAction func BT6(_ sender: AnyObject) {
//this checks when BT6 is pressed and then hides it
BT6.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}
//this part creates a randomiser between 0-4 and depending on which number turns out, it will hide a certain button
do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}



taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

}
@IBAction func BT5(_ sender: AnyObject) {
BT5.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT6.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}


taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")


do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}



}
@IBAction func BT4(_ sender: AnyObject) {
BT4.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT6.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}


taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}

}
@IBAction func BT3(_ sender: AnyObject) {
BT3.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT6.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}


taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}

}
@IBAction func BT2(_ sender: AnyObject) {
BT2.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT6.isHidden = false
} else if random == 4 {
BT1.isHidden = false
}


taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}

}
@IBAction func BT1(_ sender: AnyObject) {
BT1.isHidden = true
let random = Int(arc4random_uniform(UInt32(4)))
if random == 0 {
BT5.isHidden = false
} else if random == 1 {
BT4.isHidden = false
} else if random == 2 {
BT3.isHidden = false
} else if random == 3 {
BT2.isHidden = false
} else if random == 4 {
BT6.isHidden = false
}


taps += 1
setLabel(taps)
let defaults = UserDefaults.standard
defaults.set(taps, forKey: "key")

do {
audioPlayer = try AVAudioPlayer(contentsOf: boomSound as URL)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch {
// Catch exception
}

}


}

//interstitial Ad
func createAD() -> GADInterstitial{

let interstitial = GADInterstitial(adUnitID: "ca-app-pub-1469592343938512/2951120388")
interstitial.load(GADRequest())
return interstitial
}

Answer

This is called each time view appear on screen, not when it loads. Move that code inside ViewDidLoad method

//When view is loaded
override func viewDidAppear(_ animated: Bool) {
    BT6.isHidden = true
    BT5.isHidden = true
    BT4.isHidden = true
    BT3.isHidden = true
    BT2.isHidden = true
    //Hiding all but one button when the view controller loads
}
Comments