我想弄清楚如何创建一个弹出窗口,该窗口仅在您启动该应用程序时出现一次,然后除非您关闭该应用程序并重新启动它,否则不会再次出现。但是,如果您查看下面的代码,您会发现每次 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)
}
请您参考如下方法:
只需创建一个 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)
}
}
}