5

I would like to create the following UIAlertAction:

img1

@IBAction func buttonUpgrade(sender: AnyObject) {

           let alertController = UIAlertController(title: "Title",
        message: "Message",
        preferredStyle: UIAlertControllerStyle.Alert)

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

    alertController.addAction(cancelAction)
}

I'm aware that an UIAlertController is initialized with a title, message, and whether it prefers to be displayed as an alert or action sheet.

When the button is pressed I would like to display the alert, but alert.show() doesn't work. Why doesn't my code work?

Community
  • 1
  • 1
Cesare
  • 9,139
  • 16
  • 78
  • 130

3 Answers3

17

The main issue here is that UIAlertController (unlike UIAlertView) is a subclass of UIViewControlller, meaning it needs to be presented as such (and not via the show() method). Other than that, if you want to change to color of the cancel button to red, you have to set the cancel action's alert style to .Destructive.

This only works if you want the button to be red. If you want to change the colors of the buttons in the alert controller to arbitrary colors, this can only be done by setting the tintColor property on the alert controller's view property, which will change the tint color of all of its buttons (except those that are destructive). It should be noted that with the design paradigms that Apple has put in place, it isn't necessary to change the cancel button's color due to the implications of its having bolded text.

If you do still want the text to be red though, it can be done like this:

let alertController = UIAlertController(
    title: "Title",
    message: "Message",
    preferredStyle: UIAlertControllerStyle.Alert
)

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

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

alertController.addAction(confirmAction)
alertController.addAction(cancelAction)

presentViewController(alertController, animated: true, completion: nil)

Which produces the results you're after:

enter image description here

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • I still can't wrap my ahead around the swift syntax for closures... What's `in` supposed to mean? why is everything _inside_ the curly braces? It's going to take time to get used to it... – Nicolas Miari Jul 14 '15 at 02:12
  • `in` just separates the arguments from the closure implementation – MrBr Nov 13 '15 at 13:53
  • A good way that I've heard that helps me remember how the in syntax is used is to think, in the example above, "Action is starring IN the following function". – Tornado Apr 09 '19 at 22:58
5
let alertController = UIAlertController(
    title: "Title",
    message: "Message",
    preferredStyle: UIAlertControllerStyle.Alert
)

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

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

alertController.addAction(okayAction)    
alertController.addAction(cancelAction)

self.presentViewController(alertController, animated: true) {
    // ...
}
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
RonM
  • 187
  • 3
2
var alertController = UIAlertController(title: "Alert", message:"Message", preferredStyle: UIAlertControllerStyle.Alert)
let confirmed = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

alertController.addAction(confirmed)
alertController.addAction(cancel)
self.presentViewController(alertController, animated: true, completion: nil)

Important is the last line "self.presentViewController" to actually show your alert.

Hope it works ;)

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Armin Scheithauer
  • 601
  • 1
  • 7
  • 17
  • Thank you for answering! I would like to have a `.Cancel` button. – Cesare Jan 25 '15 at 14:32
  • I was searching the documentation but could not find anything about nullability of the handler parameter. But it seems as it is safe to pass in nil. – MrBr Nov 13 '15 at 13:54