Cesare
Cesare

Reputation: 9419

Create an UIAlertAction in Swift

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?

Upvotes: 5

Views: 26932

Answers (3)

Armin Scheithauer
Armin Scheithauer

Reputation: 600

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 ;)

Upvotes: 2

Mick MacCallum
Mick MacCallum

Reputation: 130193

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

Upvotes: 17

RonM
RonM

Reputation: 187

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) {
    // ...
}

Upvotes: 5

Related Questions