2016-10-02 302 views
-8

我用一些按鈕編寫了一個應用程序。其中一個按下按鈕顯示插頁式廣告。當我關閉廣告時,視圖上的所有按鈕消失。我該如何解決這個問題?當廣告加載時按鈕消失

//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 
} 
+1

請在問題中包含您的代碼。 –

+0

我粘貼了我的代碼 –

回答

0

這就是所謂的每次視圖出現在屏幕上,在加載時不會。在ViewDidLoad方法內移動該代碼

//When view is loaded 
override func viewDidAppear(_ animated: Bool) { 
    super.viewDidAppear(animated) 
    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 
}