gpt4 book ai didi

swift - UiAlertView 或 UiAlertController 在 Swift 中只显示一次

转载 作者:搜寻专家 更新时间:2023-11-01 05:54:16 25 4
gpt4 key购买 nike

如何让我的 UiAlertControllerUIAlertView 在 Swift 中只显示一次?

override func viewDidLoad() {

var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

if let nameIsNotNill = defaults.objectForKey("name") as? String {
self.name.text = defaults.objectForKey("name") as String
}

if let phoneIsNotNill = defaults.objectForKey("phone") as? String {
self.phone.text = defaults.objectForKey("phone") as String
}

var alert = UIAlertController(title: "Disclaimer", message: "WE STRIVES TO PROVIDE ACCURATE, UP-TO-DATE INFORMATION ON THIS APPS.", preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction(UIAlertAction(title: "Agree", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Disagree", style: UIAlertActionStyle.Default, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)

super.viewDidLoad()

}

最佳答案

import 命令下声明一个全局 Bool 变量:

var justOnce:Bool = true

你应该这样使用它:

override func viewDidLoad() {
super.viewDidLoad()
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()

if let nameIsNotNill = defaults.objectForKey("name") as? String {
name.text = defaults.objectForKey("name") as String
}

if let phoneIsNotNill = defaults.objectForKey("phone") as? String {
phone.text = defaults.objectForKey("phone") as String
}

if justOnce {
var alert = UIAlertController(title: "Disclaimer", message: "WE STRIVES TO PROVIDE ACCURATE, UP-TO-DATE INFORMATION ON THIS APPS.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Agree", style: UIAlertActionStyle.Default, handler: nil))
alert.addAction(UIAlertAction(title: "Disagree", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)

justOnce = false
}
}

关于swift - UiAlertView 或 UiAlertController 在 Swift 中只显示一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com