2017-06-08 94 views
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") 
    } 

这就是我想要放置代码

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

这里面的launcedBefore关键 更改为一个错字:

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

如果你尝试在AppDelegate中,你必须提出警告在window?.rootViewController?

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) 

检查值(true或false),您得到的let launchedBefore = UserDefaults.standard.bool(forKey: "launcedBefore")。显示警报取决于bool的值。

相关问题