Ion Verdes Ion Verdes - 11 months ago 58
iOS Question

Displaying an alert on a new View Controller

I have a button that sends me on another View Controller. What I am trying is to display an alert on the next View Controller.

Zil Zil
Answer Source

In the viewDidLoad() method of the new controller, create a new UIAlertController and display it like the following

let alertController = UIAlertController(title: "Default Style", message: "A standard alert.", preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel) { (action) in
    // ...

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
    // ...

self.presentViewController(alertController, animated: true) {
    // ...

Note that this example was taken from the NSHipster website which offers nice articles about iOS. You can find the article about UIAlertController here. They also explain other stuff you can do with that class, like display an Action Sheet for example.