Ben*_*ode 2 swift uialertcontroller
我想弄清楚如何创建一个弹出窗口,只有在你启动应用程序时才出现一次,然后除非你关闭应用程序并重新启动它,否则不会再出现.但是,如果您查看下面的代码,您将意识到每次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)
}
Run Code Online (Sandbox Code Playgroud)
小智 6
只需创建一个Bool的全局变量.如果应用程序已打开,则从false开始.然后,一旦看到免责声明,它就会将变量设置为true.然后根据变量的值显示视图控制器.
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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1622 次 |
| 最近记录: |