2016-04-25 9 views
2

私は、アプリを起動したときに一度しか表示されないポップアップウィンドウを作成する方法を理解しようとしています。 。ただし、以下のコードを表示すると、ViewControllerが表示されるたびにアラートがポップアップすることがわかります。たとえば、設定タブに行き、ViewControllerメインに戻ると、警告がポップアップ表示されます。アラートを一度だけ表示させる方法

override func viewDidAppear(animated: Bool) { 
    let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert) 
    alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alertController, animated: true, completion: nil) 
} 
+0

[スイフトに一度だけ表示するUiAlertView又はUiAlertController]の可能な重複(http://stackoverflow.com/questions/28285993/uialertview-or-uialertcontroller-to-display-only-once-in-swift ) – Caleb

答えて

5

Boolであるグローバル変数を作成するだけです。アプリが開かれている場合はfalseから開始します。免責条項が表示されたら、変数をtrueに設定します。次に、変数の値に基づいてView Controllerを表示します。

var disclaimerHasBeenDisplayed = false 

class ViewController { 

    override func viewDidAppear(animated: Bool) { 

      if disclaimerHasBeenDisplayed == false { 

      disclaimerHasBeenDisplayed = true 

      let alertController = UIAlertController(title: "Disclaimer", message: "WARNING: Wakeboarding is fun, however it can be highly dangerous. 
    Wake Dice is not liable for any injuries obtained while wakeboarding. Please ride carefully!", preferredStyle: UIAlertControllerStyle.Alert) 
    alertController.addAction(UIAlertAction(title: "Accept", style: UIAlertActionStyle.Default,handler: nil)) 

     self.presentViewController(alertController, animated: true, completion: nil) 
    } 
    } 
    } 
+0

ありがとう!それは完全に働いた – Benpeterscode

+0

この答えを正しい解決策としてマークしてください。ありがとう、 – rMickeyD

関連する問題