UiAlertView or UiAlertController to display only once in Swift

两盒软妹~` 提交于 2019-12-06 08:51:14

Declare a global Bool variable just under import commands:

var justOnce:Bool = true

You should use it this way:

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
    }
}

I modified the code in this thread so as to allow the user to "Do Not Show This Message Again". Essentially, shows the alert and gives the user the ability to disable the alert in future.

Code as follows:

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)

    }

....

}

I put it in the viewDidLoad() section of my relevant UIViewController so it loads on initial view/call of the page.

Hope this helps someone with a similar need.

Comments and modifications most welcome ;)

**** UPDATE ****

I got an error on testing -

"Presenting view controllers on detached view controllers is discouraged"

I found this excellent post that helped me resolve the issue : http://timdietrich.me/blog/swift-presenting-view-controllers-on-detached-view-controllers-is-discouraged/

Quoting the article:

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.

So I updated my code and moved the relevant block to the viewDidAppear section and all is well. NO errors.

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()}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!