Ion Verdes Ion Verdes - 1 month ago 7
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

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
    // ...
}
alertController.addAction(cancelAction)

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

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.

Comments