Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 270
Cesare Avatar asked Jan 25 '15 14:01

Cesare


People also ask

How do I create an alert message in Swift?

Adding Action Buttons to Alert Dialog To create an action button that the user can tap on, we will need to create a new instance of an UIAlertAction class and add it to our alert object. To add one more button to UIAlertController simply create a new UIAlertAction object and add it to the alert.

How do I add an alert in AppDelegate Swift?

@OrkhanAlizade create a ViewController , put your code into the ViewControllers viewDidAppear method, and in your AppDelegate , set that ViewController as the windows rootViewController (and also don't forget to create the window itself). @DánielNagy it works! Thank you! @OrkhanAlizade you are welcome!


1 Answers

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

like image 53
Mick MacCallum Avatar answered Sep 18 '22 15:09

Mick MacCallum