2017-06-08 14 views
0

私はアプリがデバイス上で開かれているが、私はコードでいくつかの問題を抱えています初めてのアラートを作成します。これは私がこれまでのところ、すべてのヘルプは、これは私がlauncedBeforeキー 変更し、それまでにタイプミスがあり、コード私はアプリが開かれた初めてのアラートを作成したい

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
+0

「AppDelegate –

+0

のdidFinishLaunchingWithOptions」機能で警告を表示しますが、コードに「何の問題がありますか? – Larme

+2

"launcedBefore"!= "launchBefore" –

答えて

0

に配置しようとしているところである

// alert first time app is opened 
    // making of alert 

    let alert = UIAlertController(title: "Navigation", message: "Tap Right Hand Side of Screen For Next Quote, Left Hand Side To Go Back", preferredStyle: UIAlertControllerStyle.alert) 

    //add ok button 
    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil)) 

    // detect if first launch 
    let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore") 
    if launchedBefore { 
    } 
    else { 
     self.present(alert, animated: true, completion: nil) 
     UserDefaults.standard.set(true, forKey: "launchedBefore") 
    } 

をいただければ幸いしたものです。

let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore") 
0

あなたはAppDelegateでそれをしようとした場合、あなたがwindow?.rootViewController?

0に警告を提示しなければなりません
var alertController = UIAlertController(title: "Title", message: "Any message", preferredStyle: .ActionSheet) 
var okAction = UIAlertAction(title: "Yes", style: 
UIAlertActionStyle.Default) { 
       UIAlertAction in 
       NSLog("OK Pressed") 
      } 
var cancelAction = UIAlertAction(title: "No", style: UIAlertActionStyle.Cancel) { 
       UIAlertAction in 
       NSLog("Cancel Pressed") 
      } 
alertController.addAction(okAction) 
alertController.addAction(cancelAction) 
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil) 

let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")に入る値(真または偽)を確認してください。警告を表示するかどうかは、boolの値によって異なります。

関連する問題