UiAlertView 或 UiAlertController 在 Swift 中仅显示一次

UiAlertView or UiAlertController to display only once in Swift

如何让我的 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
    }
}

Just add this code in AppDelegate.swift file it will work perfectly for one time alertview

=======

let AlertOnce = NSUserDefaults.standardUserDefaults() if(!changeAlert.boolForKey("oneTimeAlert")){ var alert = UIAlertView() alert.title = "Welcome" alert.message = "welcome message" alert.addButtonWithTitle("OK") alert.delegate = self alert.show() AlertOnce.setBool(true , forKey: "oneTimeAlert") AlertOnce.synchronize()}

我修改了此线程中的代码,以允许用户 "Do Not Show This Message Again"。本质上,显示警报并使用户能够在将来禁用警报。

代码如下:

override func viewDidLoad() {
    super.viewDidLoad()

....

let AlertOnce = NSUserDefaults.standardUserDefaults()
    if(!AlertOnce.boolForKey("oneTimeAlert")){

    let alert = UIAlertController(title: "Note:", message: "Some message to be displayed on screen to the user", preferredStyle: UIAlertControllerStyle.Alert)

    let DoNotShowAgainAction = UIAlertAction(title: "Do Not Show Again", style: UIAlertActionStyle.Default) { (action:UIAlertAction) in

        AlertOnce.setBool(true , forKey: "oneTimeAlert")
        AlertOnce.synchronize()

        }

    let cancelAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {
        UIAlertAction in
        alert.removeFromParentViewController()
    }
    alert.addAction(cancelAction)
    alert.addAction(DoNotShowAgainAction)

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

    }

....

}

我将它放在相关 UIViewController 的 viewDidLoad() 部分,因此它会在页面的初始 view/call 加载。

希望这对有类似需求的人有所帮助。

欢迎提出意见和修改 ;)

**** 更新 ****

我在测试时遇到错误 -

"Presenting view controllers on detached view controllers is discouraged"

我发现这个很好的 post 帮助我解决了这个问题:http://timdietrich.me/blog/swift-presenting-view-controllers-on-detached-view-controllers-is-discouraged/

引用文章:

Notice that I was trying to present the UIAlertController in the class's viewDidLoad function. And that's the problem. You see, at that point, the view has loaded, but it hasn't appeared yet. I believe that's why the warning is referring to a "detached view controller."

The solution that I found is to present the view controller not in the viewDidLoad function, but instead in the viewDidAppear function. At that point the view has loaded and is being displayed.

所以我更新了我的代码并将相关块移动到 viewDidAppear 部分,一切都很好。没有错误。