High again in this assembly going to work on alert controllers view the contours to pop some sort of
a dialog box to our users so users can based on that make a decision when something goes wrong or a
user has to log into something or perhaps many use and has to enter some sort of text value.
So let's get this stuff and see how we're going to do that.
And when I go to explore I'm going to start a new project I'm going to call it I learned controller
app and to produce our Earth controller.
We don't actually need to do anything in our storyboards so I want to jump right into a view controller
and start programming.
The basics of it are actually rather simple.
And when I come in here and usually you want to instigate your alert controller using some sort of event
such as when the user taps on a button or vendor some data is read and finished from a network.
So it should be essentially initialized after something happens in here just to simplify my life and
lower the amount of code I'm going to put in the touches and that.
But you can imagine the very same thing applies in very ever whether you're pressing a button or you're
waiting for a function to finish in any situation you can do all of these things.
Now I don't need my view did Lord someone delete that.
So I have less code and less stuff here for the first part of it is going to be producing a new alert
controller.
So I'm going to say let my alert to be UI alert controller.
I'm from my other controller.
I'm going to have to initialize it with some settings.
I'm going to initialize with this one with the title with a message and a stylus.
I'm going to say let's format everything properly.
And I'm going to say the title of it is something happens and then the message of it is what would you
like to do.
Imagine this is a message that says your network connection disconnected or you are now logged out or
anything of the sorts.
And then you have a style under two primary styles for it.
One of style is an alert that pops in a middle of the screen and the other one is an action sheet which
essentially produce hits from the bottom of the screen it slides in from the bottom of the screen.
I'm going to use both of them.
So we get a sense of how they work.
So the first one is alert now that they have an Earth controller I can present it in my view controller.
So I'm going to go ahead and say South dot present and in the present I'm going to say present my alert
and do it animated true.
I don't do anything thing at completion.
So let me it's like this.
And that's all we have to do to make a very very basic alert controller.
Let's run this application.
Obviously this doesn't do anything for us just yet but it shows us the foundations of what we have to
do so here it is.
Any time that they touch on this screen I should get this alert.
So I tap on the of the screen shows up.
It says something happened.
What would you like to do.
And unfortunately because I'm a very good programmer I don't have any buttons there's no way to dismiss
it it's just stuck there.
So let's go ahead and fixed all of this stuff.
And before I do that you might have noticed that I'm using a different simulator and theoretically it
doesn't really make any difference up until this point.
And for a long time coming actually it doesn't really matter which kind of the simulator you use.
I used to use iPhone 8.
Now I am just like with iPhone ten because I was doing some project but we won't have anything that
is specific to iPhone 10 or iPhone 8.
If the time comes I have to do something there's a specific to iPhone 10.
I will mention it to you and I will remind you about that.
Otherwise you can continue with the one your heart or you can switch it up in here.
And if you don't remember you can go to a window devices on simulators and in here in the simulator
as you can choose the simulator that you wonder you can add the wand.
Now back to our alerts controller.
The first thing I need to do is I need to add an action to it.
So I'm going to say let this call this one.
OK action.
Let's say you have an okay action and a council action.
So okay action becomes a UI alert action.
And for the action I will again need to initialize it with some settings.
So the first part of these settings is the title that says OK whatever you're asking me I'm fine with
it.
And then we have a style that I'm going to put this side that default and then we have a handler which
essentially says if you press this okay by then what should they do.
Which is very very important.
So I'm going to say let's call this one my alert action or my alert in here and then say if that did
happen let's go ahead and say I'm not going to do anything right now I'm just going to say print you
tap.
Okay maybe later we'll do something a little bit more or more elaborate on it.
No they have this new action.
I'm going to go ahead and save my alerts that at action.
I don't want to add the okay action to it.
And that's all we have to do.
So let's go ahead and run and this time that our pop up shows up we should have a button that says OK
and then based on that we could decide about what we want to do now.
I want you to pay attention to something that built into any bar.
Then there is the dismiss.
So you don't have to manually dismiss the alert.
So when you tap on it it automatically does dismiss.
Let me clear my console so we can see what we're going to see.
And when I tap.
OK.
And yet it says your top.
OK.
I mean here the interface actually disappears.
It dismisses the alert controller.
Now this was all good.
Let's add another one to it.
I'm going to add another action saying my alert died at action and I'm going to make the action on the
fly saying you I alert.
Action you I alert.
Action and that is going to be a string of feed is canceled.
The title of it the estate if it is canceled is nine.
It doesn't do anything.
It just dismisses the view controller which is the alert.
So this one actually does something for us.
This is the part that we are interested in knowing what happens when we stop.
This is the one that we just to dismiss the alert controller.
Now if I run this application now you can imagine there are two buttons.
One of them is saying OK one of them is saying cancer.
Most of them they do dismiss.
But one of them also does something so the last thing I want to do in here is I want to add another
entity to my alert controller and that is a text field in case you want it to add a value and you want
to allow your user to add content to it.
You could essentially subclass the alerts controller and add anything that you want into it.
But that doesn't seem like a very very good idea.
The default behavior is a title a description on some text fields.
So let's go ahead in here and let's do this before my actions even though it doesn't matter that I'm
just going to make sure that happens right after the alert and here I'm going to say my alert dot add
the text field and then it says well the text with you want to add Give me the configuration for it.
I'm going to call this one name next to it.
Let's assume that you have a named expert and we have a password text with I'm for this name takes me
that way I just changed some of its attributes that say Name text with DOT text alignment becomes an
ethics alignment dot Senate and that's about that you can you can you know infer the rest of it yourself
that you can add anything to the normal text print has into here.
So if you're on the application now we will get an alert with two buttons as well as a text with which
is which is great.
So in here if I touch we get a text field and two buttons as you can imagine the size of the bygone
color and everything about this text with you can manually modify it maybe add one more sitting in here
to his more easily viewable.
So I'm going to say a name that font is you.
I find that system font of size will increase it to 24.
So it's much larger it's more visible.
Now the question becomes if you had this text with hard we're going to get access to them at any point
and that point primarily is venue press either of these buttons.
So in here when I press the okay.
But then I want to find out what user has entered on the text with someone to come in here and say you
typed OK I'm going to have another print I have other print says and 60 for the years.
I'm willing to say it is my alert.
There are text fields and it doesn't autocomplete for me text fields the my Oh I don't know why I named
this argument my alert this argument should be called anything is basically a part of the closure but
any name other than my alert would do.
I'm sorry about making such mistake.
Ah this is the argument of the closure which is essentially the name of the returning action so you
can call it my alert action or anything such as that but if you call with my alert is gonna cause a
confusion between this argument and the alert that you have up there.
So if you have followed my steps and you have made a mistake because of me please change that to any
other name.
So here when I will print the text with I'm going to say my alert the DOT text fields and that's an
array from there I'm going to get the very first one and I'm guessing I know the text fields actually
exist because I have added one and then from there I'm going get the very first ones text.
So the value of the text fields 0 8 dot text is what I'm going to be interested in printing every time
we tap on the okay button.
So let's go ahead and run this application and now when the upload every time I tap something pops that
I can clear my console.
I says something happened.
What would you like to do.
I'm gonna say hello and head okay.
It says your tap on the text with value is hell.
This is all we needed to do.
And then depending on what you're trying to do we can change this.
You can have two of these text fields and then really quickly say this one is actually called pass to
the Eiffel tee and then I'm going to put it in here and maybe pass is secure text entry is true.
And if I run the application now we should be able to see two text fields one of them is for a name.
The other one is for a password.
You can obviously add place holders or other things in your text with or to make them more easily understand
develops with us the name and the password is her law whatever.
So that when you press the cable then you want to read both of these and then make a decision.
Have we entered the correct credentials or not.
Or whatever it is that you are trying to do.
So right now when we press the cable that we get your top Okay that takes with the first one is Amir
and then that's pretty much all there is to it.
To making an alert controller making it show up as well as adding different actions.
Now I did promise that I will show you how an action shit alert controller will work and that is why
coming to an alert and seeing this style is actually not alert rather action.
Shoot now however you have to keep in mind that the action shoot as the name implies it only accepts
actions so action shots can not have text fields because of that.
I'm actually going to comment this far out when I comment this far out.
So now we have an action sheet that has two buttons on it.
Let's run this and we now should see an alert controller that essentially a slight itself in from the
bottom of the screen.
So when it comes in.
Yes a tap.
Action sheet slides in from the bottom.
Let's do it again.
And that's how it works.
And from time to time this is more useful.
This is what you want to have on time.
Other times you want to have a dialog box right in the middle of the screen so that's about it for alert
controllers and let's move on to our next lesson.