Part 9 — Working with UIAlertController

Dhruvik Chevli
Hashtag by IECSE
Published in
4 min readMay 31, 2020

IECSE Crash Course: Development with Swift

This article is going to be the ninth article of the series IECSE Crash Course: Development with Swift. Through the course of 10 articles, we are going to make a small app which will display a set of users that we will fetch from the net. While making the app, we will cover some of the most important topics that every iOS developer must know. You can get the code till the previous article here.

In this tutorial, we’re going to go through one of the building blocks for any app, the UIAlertController. This is the class that helps us in showing alerts, responding to user actions and taking text inputs.

According to the Apple Documentation,

An object that displays an alert message to the user.

Alerts

The first step of using the AlertController is to instantiate it.

let alertController=UIAlertController(title: String, message:String, preferredStyle:UIAlertController.style)

The title, as the name suggests is the title of the AlertController that will pop up on the screen. Message is the description for the AlertController.

AlertControllers help us in presenting alerts and Action Sheets, Action Sheets are another specific type of alerts that appear in response to a control or action and presents a set of two or more choices related to the current context. We’ll have a look at ActionSheets in the latter part of this article.

In our Users app, in SampleScreen.swift, we are going to use the UIAlertController to handle the errors when the User doesn’t fill in all textFields properly.

addAction(_:) is where we add the buttons/actions that the user can take. The UIAlertAction takes 3 parameters, title, this is the text that’ll go on the buttons. Style has 3 enum options,

  • .default.
  • .cancel, this is used to give the user a choice to go back to the previous screen if the user has made some mistake,
  • .destructive, this makes the text in the button appear in red, its supposed to tell the user that the action that’s going to be taken is dangerous and it cannot be undone.

The next parameter is the handler, which can either be a closure which will be passed or you can pass a function too.

Try and make the title of all the actions as concise as possible as there is not much space on the buttons in an AlertBox.

In the last line, we present this AlertController on top of our viewController.

Now, if we run the app and Press the Add User button on our Sample Screen without pressing filling all the fields, we get an alert that looks like this.

We can add more actions with addAction function.

Make a note of how we use different styles for each action here.

As you can see, the action with style cancel always stays at the bottom irrespective of what order you add the actions in.

Adding TextFields

We can also add textFields to our alerts and ask for input from the user and then handle them accordingly.

Action Sheets

An action sheet is a specific style of alert that appears in response to a control or action and presents a set of two or more choices related to the current context. Use an action sheet to let people initiate tasks, or to request confirmation before performing a potentially destructive operation.

We can use an action sheet by changing the preferred style of our controller to .actionSheet.

Adding actions to the controller is done in the same way that we did before.

Action sheets have three benefits over alerts:

  • They slide up from the bottom of the iPhone screen, so you can reach the action sheet buttons with your thumb. Easy!
  • You can fit more text on the button, compared to the alert style.
  • Usually, you need less text than an alert, because the action sheet buttons are self-explanatory.

Wrapping Up

We’re only going to keep the alert popup we wrote in the beginning in our app. You can get see the final project over here. In this article, we’ve taken a look at the different ways that we can work with UIAlertController. You can continue with the series in the final article where we learn about storing data!

Congratulations 🎉! You have just finished the 9th tutorial!

All of the above-written code is available on Github. Watch this space for more interesting challenges, up next in this series!

Confused about something? Let us know in the responses below. 😄

--

--