In iOS, popup messages are called alert controllers. In this video, see how to create alert controllers and how to make them appear on screen.
- [Instructor] The alert controller is one of the most common user interface elements in iOS. An example of an alert controller can be seen in the context application when you tap the button to send a message. If the user has multiple phone numbers, as this person does, you'll see those multiple numbers pop up along with a cancel button. This is an example of an alert controller. An alert can appear in this way, which is called an action sheet, or as an alert which appears in the center of a screen.
Let's take a look at how to create these. I'll cancel out and head over to Xcode. In Xcode I have a blank project and I'm going to add a button to it in my storyboard. So I'll click the library button, filter for button, and double click on it to add one to the screen. I'll change the text of the button by double-clicking it typing out show alert and I'll hit return to commit the change. And that looks like a good spot for the button. Now let's open up the assistant editor and get that view controller class on the right side by option clicking it.
So option clicking view controller dot swift, and I see it on the right and I have my storyboard on the left. Let's right-click and drag from the button into the code file and then for the name, I'll call this show alert. So this is going to be a method that's called when the button is tapped. So hit connect, and there's the show alert method. Let's go back to the standard editor. And head over to view controller dot swift. In view controller, I'm going to go inside the show alert button, and I'm going to create an alert and present that alert.
So let alert equal UI alert controller, and then we create it with a parenthesis, head on down to the one that says title message and preferred style. Once you choose that, you can set the title and message as strings. So with this first one highlighted, I'll type open quote and Xcode automatically adds the close quote and I still have the string highlighted so I can type the string in right now. So this will be alert, title, just so it's easy to tell which one's the title and which one is the message.
Hit the tab key to head over to the message parameter, do the same thing. One open quote and then just type out the message. Alert message. Tab over to preferred style, and this time we'll type a dot. And you can either choose action sheet, which we saw in the context application, or alert. So both of these will work the same, you can choose whichever one you want. I'll choose alert for now. Now what we want to do is present the alert to the user. To do that, we call the present method which is part of the UI view controller class.
So since our view controller class is a sub-class of UI view controller, we have access to that method to present a view controller on the screen. To do that, type present I'll just press return to have Xcode fill that out for me. And for the view controller to present, pass in a UI view controller. Our alert happens to be a UI view controller. It is a UI alert controller, but that inherits from UI view controller just like our view controller class does.
So I'll press return right there to have Xcode finish writing that out for me. And then tab over to animated. We'll type true for that. And then for completion, you can optionally write some code that runs when the alert is dismissed. Now typically, I don't handle this method I just set it to nil, meaning do nothing when the alert is dismissed. The reason for that is, if I want to do something when the alert is dismissed, I'm going to handle that with the alert actions.
So an alert will have a cancel button, for example. If you want it to do anything when the user presses cancel, you attached that code to the cancel button rather than attaching it to the present method. So this is always there for you to handle should you choose, but for the most part, you might not find yourself using it. Let's now run the application and see what we get. So here's our app with the show alert button. Once you tap that button, you should see the alert on the screen. So you have the alert title in bold, and the alert message in standard font.
And there's no button to dismiss the alert. In fact, if you click around it the alert does not go away. So let's take a look at handling that in the next movie.
- Alert controllers and actions
- Sliders, progress bars, and switches
- Creating UI elements with code
- Displaying web content with Safari view controllers and WKWebView
- Creating collection view controllers
- Creating tabbed apps
- Publishing your app with App Store Connect