Lets Start With The Swift Documentation
Use the UIAlertController class to configure alerts and action sheets with the message that you want to display and the actions from which to choose. After configuring the alert controller with the actions and style you want, present it using the present(_:animated:completion:)method. UIKit displays alerts and action sheets modally over your app’s content.
In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction(_:) method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Listing 1 shows how to configure an alert with a single action.
UIAlertController Step 1
To start, create your UIAlertController by declaring in your button action. The title displays at the top of your controller, the message will display under it, but you don’t need to add a message, you can set this value to nil if you’d like. The preferred style can be either .aciton, or .actionsheet. Action will be displayed modally, and is the most common type of alert you’d be used to seeing while on your phone, while actionsheets, which are presented from the bottom are more commonly what you’d probably see while browsing on your iPad.
However, its not enough just to declare the UIAlertController, you also need to make sure you present it. If you’ve given your alert controller a name other than alert, make sure you present that instead.
- This is what you should be seeing at this point if you’ve done it correctly.
UIAlertController Step 2
Presenting an alert alone would probably be useless to most apps, even if your goal is only to display information, you’d still need a button that dismisses the alert to get rid of it, so lets dive into adding actions to your UIAlertControllers.
Declare the action as UIAlertAction. Give it a title that represents it’s function. Here the options for the style are .cancel, .default, and .destructive. Using cancel will your action a look that implies no action will be taken, while .destructive will give your users the impression that this is a warning, something is probably going to be deleted, and .default will just give your button a generic look that says hey, I’m just a regular old option. For this action, we’ll use .default.
Press enter on your handler to create the closure. The closure is where you’ll add what your action actually does, and make sure you give this the title of your action, and don’t just call it action, unless in fact, your action is called action.
Remember scope, if your action is accessing something outside of the scope of the action, you’ll have to add .self to be able to get to it. For instance in this example, my action is trying to access my showAlertControllerButton. After you’ve created your action, use alert.addAction to add it to your alert controller before you present it.
2. If you’ve done this correctly, your alert controller should now have your action, and be performing it when you click!
Now that we’ve added some actions, test of the differences between actions and actionsheets. This is what an action with a cancel action added would look like, versus what the same lines of code would look like as an actionsheet.
Try adding multiple buttons to your alert controller. Test out the cancel, and just as a bonus challenge, see if you can figure out how to add a textfield to your alert. Hint for the .cancel action, you can leave the handler as nil because its not actually performing an action!