How to use UIAlertController in Swift.

We’ve all used a UIAlertController once before.

I’ll teach you how to easily implement this into your Swift apps.

It looks like this:

Implementing UIAlertController is pretty straight forward.

We can start by creating an instance of UIAlertViewController with the preferred style set to .alert.

Then we will present the controller by calling present.

Command + R to see the alert.

Let’s add some actions (buttons) that will run code when pressed.

There’s different types of style’s:

  • .default
  • .destructive
  • .cancel

I’ve added all three as a demonstration, once pressed it’ll print() the chosen button.

To use action sheets, change the preferredStyle to .actionSheet.

That’s it pretty much it. You should be able to implement this into your projects. 😀