Alert Messages


Advanced iOS Features
Storing Data
PREVIEW11m 37s
Second Screen
14m 39s
Start course
1h 57m

This course explores some advanced iOS features. We'll look at how to store data permanently. You'll learn how to work with more than just one screen, how to use Gesture Recognizers, Timers, Alert Messages and so much more. You're going to have essential skills for iOS development after you complete this course.

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.


To the most out of this course, you should have some basic understanding of programming and computer science in general.



Hi. Within this lecture, we're going to see how to create Alert Messages. So, these messages are just small pop-up messages when we try to give some alert or some message to the user so they can see what's going on and they can take some actions regarding to those messages. So, in order to do that, I'm going to create a Single View App and I'm going to call this project AlertProject or something because we're going to create a registration form in here in which we will have the username and password and password two. So, if any of this information is given incorrectly, for example, if user leaves the username blank, we will display an alert message saying that you have to give a username. So, this is a real functionality that we will be using during, we make real apps in the upcoming lectures, upcoming sections. So, you have to learn how to create these alert messages. In all of the apps, you will need those. So, let me create this design first. I'm going to start with Text Field in which I will get the username, password and password two. So first, let me get the username. So, let me make the central. Let me go to Attributes Plain and we have to go to Placeholder and say username. So, user will know they will give username in here.

And after that, I'm just going to copy and paste this with control at command c and command v. This will be our password text. So, I'm going to say password and then we're going to copy and paste this one more time. So, this will be password again. So, we're going to check if the first password matches the second password. So, this is a classical registration form you see in this kind of apps. So, if these do not match, then we will display an alert message as well. So later on, I'm going to go for a button. So, this will be our Register or Sign Up or Save button. So, let me make this a little bit bigger and I'm going to call the Sign Up. So, user will sign up using this button and that's it. So, maybe we can make this a little bit central. And even, we can just drag a label in order to have a title in our app so we can call this Registration App or Alert Message App so that our app will look a little bit better. So, let's say the User Sign Up. So, this is a basic user sign up view controller that you can use in your own projects. So, let me Reset to Suggested Constraints and then we're going to open our ViewController.swift in an another assistant editor. And we're going to control drag and drop and just define those text fields. We don't need this label.

We can just go for this text field. So, for text field I'm going to go for the username, text. So, let me get the second one. We will call this passwordText and then we can go for the third one which is password two text or password again text. So, I'm going to call this password2Text. So, for the last one, I'm going to control drag and drop this button as an action and I will call the signupClicked. we go. Okay. Here we go. Now we can close this down and we can actually go and head back to our ViewController.swift because we're going to create our first alert message. So, let's do that in signupClicked function. So first of all, I'm just going to show you how to create this alert box. When you click on this, it will just display it. lLater on, we're going to do our if controls or if statements. And in order to create alert message, we're going to create a variable called Alert and of course we're going to use a class for that. It's already built in in the swift so we can just get that and use that in our project. So, it's called UIAlertController, not UIAlertAction but UIAlertController. So, we're going to see what a UIAlertAction is later on. Right now, we have to create this UIAlertController. As you can see, this is an object that displays an alert message. If you open parentheses, you will see the required parameters like this.

So, we're going to go for this third one, the parameters of title, message and preferred style. So, title is actually the title. What will be the title of our message? Something like Error! will do the job. Now, we can go to message. This will really display a message in an alert box. So, I'm going to go for "Username not found!", for example. So, user will see this message in another alert message box. So for style, if you click enter over here, you can choose for different styles. So, if you click enter and hit ., you will see that it's going to be either an action sheet or an alert, and we want alert in this case. So, action sheet is kind of a menu but we want alert. So, again, we have created this UIAlertController. Now, we can present this to users by saying self ViewController. So, self refers to ViewController. Remember, self.present. So, if you say present, it will ask you what to present. So, we want to present this alert that we have created. So, you can just say alert here. And if you create some other views later on, you can present it using the same technique. So, I'm going to go for alert, and is it going to be animated? Why not? Just say true and completion. It asks for if I'm going to do anything when this operation completes. I don't need that kind of thing right now. So, I'm going to say nil and we will have some kind of completion later on in this lecture.

Don't worry about it. So, let's build this and see if we can get this as an alert message and let's test if this will do the job or if we will make another adjustment over here. So, if I click on Sign Up, as you can see, I get the message. So, this is working but I cannot get out of it so wherever I click it doesn't disappear, so I don't have any button right now saying OK or cancel, go back. I don't have that kind of thing so I cannot go back right now and this is not what I want, so this is displaying so it's okay but we have to add some new things to it, like we have to add a button. So, in order to create this button, before we present this alert message, we have to add an action to our alert. So, we're going to say this is going to be a okButton, for example and for okButton okButton, for example and for okButton, I'm going to use UIAlertAction and this UIAlertAction again will ask me for a title and the style and a handler. We're going to see what a handler is. So, title is going to be the text that we will display in the bottom. So, this is going to be something like OK or cancel, go back, I understand. So, I'm going to go for OK. So style, we have different kinds of styles here. If you hit Enter and hit ., you can see those styles like we have cancel style. We have default, destructive. You can try these styles, it shows the button in a different way.

If you use those for example, if you're going to destruct or if you're going to delete something, you can go for destructive. If you're going to cancel the operation, you can go for cancel. This only changes the display of the button as I set, so it doesn't affect the code or it doesn't affect the action itself. So, I'm going to go for default but later on, you can try to go with cancel and destructive to see how does that look like. So, in the handler. So, what do you want to happen if someone clicks on this button? So, this is where you write your code. In order to do that, you have to hit Enter and if you don't want something to happen, if you just want to dismiss this button, you can say nil here as well, but I'm going to go for that and hit Enter. It will create a UIAlertAction in here. So, this is a variable if you want to interact with the UIAlertAction itself. So, I'm just going to go and hit Enter and the part that we're interested in is the coding part okButton. So, as you can see it shows us with code. So, whenever someone clicks on this okbutton, if I want something to happen, I will write inside of this code and this will be called using this variable. So, this generates it for itself. All you have to do is just hit Enter when you see the handler itself. So, I'm just going to say button clicked and let me just do a print statement here. print("button clicked"). So, we can actually see it in the logs. And later on, since I created this OK button, since I created this UIAlertAction, I have to connect this to my alert itself and in order to do that, I will say alert.addAction.

It will ask me for a UIAlertAction, I'm going to say okbutton. So, that's good. I believe now we are ready to test this actually So, let's run this into simulator and let's see how it looks like. So, we're going to see the same thing again, we're just going to go and sign up and as you can see now, we have this OK button over here. If I click OK, it will dismiss the message and you will see "button clicked". So, whenever you create an OK button, if you use an handler in this handler, you can write whatever you want it to happen after this message is dismissed. So, button clicked works and that's all for right now. We have seen how to create an alert message. Now, it's a little bit challenge for you. I'm going to suggest you pause the video here and you try to come up with an algorithm that says "Username not found!" if usernameText is blank. And if it's okay, it's going to check for the password text and if password is missing, it will say "Password missing" and then later on it will check to see if passwordText and password2Text do not match and it will give a related error message. So, you're going to have to create a Sign Up Registration Form with alert messages that just controls if usernameText given, if passwordText given, if passwords actually match. So, pause the video, try to do it on your own and if you can do it, that's great. If you cannot, don't worry, we will do that together in the next lecture.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics