How to present a warning using UIAlertController

I am trying to present a warning using swift. This is the code I used from viewDidLoad, but nothing happens when the code runs. Can anyone help?

    var alert = UIAlertController(title: "test title",
        message: "test message",
        preferredStyle: .Alert)
    self.presentViewController(alert, animated: true, completion:nil)
+4
source share
2 answers

You must present any view controller after the parent view appears. Put the presentation code in the viewDidApear method.

override func viewDidAppear(animated: Bool)
    {
        super.viewDidAppear(animated)

        var alert = UIAlertController(title: "test title",
            message: "test message",
            preferredStyle: .Alert)
        self.presentViewController(alert, animated: true, completion:nil)

    }
+15
source

Show AlerView in swift for ios8

func showAlertVIew(){

        var alert = UIAlertController(title: "Info", message: "Welcome to Swift world.", preferredStyle: UIAlertControllerStyle.Alert)

        let alertOKAction=UIAlertAction(title:"OK", style: UIAlertActionStyle.Default,handler: { action in
            println("OK Button Pressed")
            })

        let alertCancelAction=UIAlertAction(title:"Cancel", style: UIAlertActionStyle.Destructive,handler: { action in
            println("Cancel Button Pressed")
         })

        alert.addAction(alertOKAction)
        alert.addAction(alertCancelAction)

        self.presentViewController(alert, animated: true, completion: nil)
    }
+2
source

Source: https://habr.com/ru/post/1545544/


All Articles