Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to present an alert with UIAlertController

Tags:

ios

swift

alert

I am trying to present an alert using swift. This is the code I used from viewDidLoad but nothing happens when the code is run. Can someone help?

    var alert = UIAlertController(title: "test title",
        message: "test message",
        preferredStyle: .Alert)
    self.presentViewController(alert, animated: true, completion:nil)
like image 561
user3764586 Avatar asked Nov 30 '22 01:11

user3764586


2 Answers

You must present any view controller after its parent view has been appeared . Put your presentation code to viewDidApear method.

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

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

    }
like image 126
Yatheesha Avatar answered Dec 21 '22 22:12

Yatheesha


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)
    }
like image 26
Ravindra Singh Avatar answered Dec 21 '22 20:12

Ravindra Singh