So far we had a look at some of the delegation that we developed ourselves.
So we wrote our own protocols.
We conformed the class to that and then we use that to pass a message.
Now in this lesson we're going to look at some of the delegations that exist in different classes in
the U.S. almost every class in the U.S. has at least one delegation and then some of them they have
a lot more than that.
So let's have a look at doors in export and what looks good on that to start a new project.
Iowas application that is actually a good time for us to take what we learned into an actual application
not a single view of un-Godly.
That's why if I'm going to say text field the allegations are swift end in here I'm going to go ahead
and say my a storyboard.
I have one text that's the only thing that they want to have in here.
Here's a text that I'm actually going to go ahead to the same thing as usual no borders.
Make sure the background is something that you can actually view it and that takes a little bit larger
have a placeholder saying for instance a value line at the site and all of these things and all you
can do within your own whatever you feel like more comfortable.
So the next thing is I'm going to have to make an outlet for my text.
So same thing as before.
ASSISTANT EDITOR IN MY a class like this one and call it my text file.
So so far we haven't done anything that is different.
We have made a text field and get that.
And now I know one you will know soon as well that most outlets in your interface builder they have
built in delegates and you can call those delegates by doing either of these two things.
Either hold the control button and drag them over your view controller in the window that pops you could
say outlet delegate's which means I'm delegating from my text field to this view controller.
So let's do that.
Or you could actually do it through the programming.
I'm going to do it in both sides so we see both of them.
So the first one was holding control and dragging over our view controller and saying delegates that's
one way of doing it.
I'm actually going to go ahead and disconnect it so I can do it one more way the other way was this
way I go to my view controller and in my view controller I'm going to clean everything up like usual.
And in my view that law the moment of you know what I'm going to say my text with that daily guest becomes
stuff becomes this one.
Now if you recall anyone that we wanted to assign that delegate to it had to conform to that delegation
protocol which I'm currently not doing that so I'm going to go to my class viewer controller.
So for a class you I view controller and I'm going to say you find a text field that delegate's so and
conforming to you why feel delegate as well.
So now my view controller can accept delegation methods from my textbook.
But what are those what are the different functions or properties that we can use from the sky.
Let's go ahead and google it up.
I'm going to go to my browser.
Say Give me your text textfield delegates and go look at the text with delegate and soon enough we will
learn better ways of finding this matters.
But for now I'm going to find those.
I see that in the delegation you know everything is stuff I have textfield should begin editing takes
with did begin editing.
So this one gets triggered the moment you start entering a value or it says the one I'm looking for
actually is something called text which should return.
Let's read that up and it says Ask the delegate if that takes which should process the processing of
the return button.
So weatherman we had ridden by then something should happen or not and I'm going we use that.
So it is called textfield should return the squarehead scope.
And as long as you are programming in Swiftie can just type a little part of that code and will automatically
auto complete it for you.
So I'm going to say textfield Should that.
And it says or should return.
Is that the one you're looking for.
And as you can see the return if it is a boolean.
So I'm going to say return true.
So yes it has to do something but what should it do.
So let's go ahead and quickly talk about that a very very simple case of this would be this.
I'm going to go to a storyboard and I'm going to select my text and when a change return returned then
to look like a done by then.
Even though this isn't really necessary I like to do it so it kind of becomes more clear what you're
doing.
So a return key is a donkey and I'm going to say the moment that textfield should return function is
a trigger before you return true.
Do this textfield resign the first responder.
And I'm sure you can guess what this is going to do for us.
Is it going to essentially say every time you press the return button on this textfield get rid of the
keyboard and then yes that should happen.
So I'm going to go in here.
I'm going to.
Show me the keyboard by pressing on the screen.
And if you don't see the keyboard is because your soft keyboard is not on.
You have to go in here on Press command OK.
So if I have command Cain here the keyboard disappears.
If I had command again it appears and testing purposes doesn't really matter but when you're using the
simulator keep in mind keyboard shows up here.
So I'm going to say let's say high on had done and gets rid of the keyboard now you might be asking
yourself so what is this text field argument that is in here that essentially calls that text field
that has triggered this function.
You can simply imagine that this may not be the only text field that you have in your interface.
You might have two of those and then depending on which one is doing something you might want to do
a different thing in the next example.
I'm actually going to do that.
I'm going to go ahead in here I'm going to use a different delegate function called Text field.
Should change should change which is this which essentially says Should I change the value that you
are typing in a text field or not.
Now let me quickly explain what is this useful for.
Imagine a situation like this and we saw this sometime in the past that we wanted to have a text field
that we can only enter numeric values.
So two three to four would be valid values but effort would end.
So we can actually use this function textfield should to change Kerak there to decide whether it should
actually change or not.
So practically by the end of this exercise we should be able to do this.
If I were going here I'm press tree that should appear.
But the 5:01 press pass that shouldn't appeal it should just be returned as a false value.
So let's see how I'm going to do that.
The first thing I'm going to do is I'm going to get myself a little bit more space and then I'm going
to go ahead and say this is the logic that I want to do.
The logic is this.
Let's get all non numeric values.
So these are things such as a s b c the com dot dot I all the star and whatever.
Anything that is non numeric value that you can use get by a keyboard.
Then I'm going to say let's get our string this string.
We are trying to enter and divide that by these guys.
What does that mean.
Let's say a string is this.
Those are the numbers.
And then H-G some random characters and then those numbers.
All right if I go ahead and divide that this string by the characters that are here then non-numeric
characters we call that one card.
If I divide it by non-numeric character I have this I will have the first part which is that I'm dividing
by disguise etch j u s f d g.
And if I had one more you know percentage in here.
So that would be the first part and then I would have the next part which is four five six which is
that and that I would have that last part which is a 78.
So if I break my text against these dividers then non-numeric characters I will end up with this.
The next thing I'm going to do is I'm going to say let's join all of these things together which becomes
our previous pasted there.
All of them together then I'm going to say compared the original text.
This one against this new one.
If they all the same it means I didn't have any non-numeric character.
It means everything was numeric.
But if they are not the same it means that something has gone wrong and I have an unusually quiet you
and I should return true.
So that's the logic of what I want to do.
Let's go ahead and actually do the the first thing I'm going to do is this.
I'm going to say let's call it numbers.
Becomes are all of these guys which is very simple so we first get all the numbers all the things that
we wouldn't like to have.
Right.
Then the next thing is I'm going to say let's let's call it.
Remove the heart set.
So this is the characters that I would like to remove and that's going to be on.
And this character sets the targeteers in numbers.
So that is my character set but this is the actual character set of the numbers not the ones I want
to remove.
So because I want to remove them I'm going to say well you know what.
Get these guys and then do what we call inverted.
So reverse it.
Get these guys and then find everyone who is not to these guys because I could go ahead and manually
try to type those but it would be a very very long set of characters.
It would be a b c d e f g Hosh.
All the way to Z and then the capital letters of all of these things and then every value that is non-numeric
such as these guys so on and so you would become a very very long string and it would be very tedious
to find them.
So instead I said let's get the numbers on.
Invert them.
Once they have the inverted version I'm going to divide my a string Vider.
So let's call it divide and this thing I want is this one the input string that the text field is providing
for me.
So that's going to be very simple.
I'm going to say Let's call this one or a split or separate and separate it becomes a string of dot
components and separated by remove characters that are separated by doors.
So by now I've got all of these pieces separated and by now I have this data guy and that one on that
one because I'm separating against a character set which is anyone but these.
So it really gets rid of that part.
And at that point.
So then I'm going to go ahead and do what you suggest.
I'm going to join them back to each other.
So let's call this the join and the join is actually going to be let's call it join the string that
becomes anything that we had here.
Separate it separate that just joined and then it says how do you want to join in three.
And I want to join them using a separator.
What is the separator.
Nothing.
I want to join them right to one after another.
I don't want to join them for instance by a space.
If that was the case it would become something like this.
But they want to join them exactly the way they are put them one after another.
So I get that part done and then the next thing I'm going to do and when I say well I have that one.
Now effect here is that if you have a string that was the original one is same as joined which is essentially
this comparison.
Then you have to return true and else you have to return false.
Now let's see quickly what we did once again off the top.
All right I'm going to get rid of all of this and when I say textfield should change tack then inset
is a method that gets called from the textfield delegates.
And it says every time you change something I'm going to call this function I'm going to fire this function
but it's going to be up to the return of it whether I'm actually going to make that change or not.
Now for me I'm going to go ahead and say these are the numbers.
This is the characters that's built from those numbers inverted.
So I get everybody who is not to these guys get my text string that is received in here separated by
these characters.
So if there is a number just keep the numbers separated by those values combine them back with each
other.
And once you combine them back and I think I need to be like that once you combine them back with each
other compare them against each other.
If it was the same as it used to be at the very beginning it means nothing changed.
If it wasn't returned Fox.
Now I was going to change this little bit to a much easier syntax.
I'm going to say this is a true or false statement.
If a string is joined and then if it is true we're going to return true if it is false return false.
So what if we just return this guy that's it's going to say to return a string.
Is it equal to joint.
And if it is true it is going to return true if it is for us is going to return false.
Now let's set this up in practice.
I'm going to load up the app in my simulator and in here we should see if I'm going to boot up the keyboard
when I try the text values.
Nothing shows up then I go to the numeric ones everything works.
Any one else.
Nothing happens only numeric values and numbers can work.
So we could actually combine this by saying my text field dot keyboard type is a keyboard type dot numeric
keypad or a number power or something like that.
And of course on the simulator you can not change the type of the keyboard but on actual device you
could switch back to a different kind of keyboard but here we make sure our user absolutely enters numeric
values.
So that's about that using the delegation and finding the kind of you know functions that you have in
a certain delegate and how you would use them.
Like I said in towards the end of this section a few lessons from now I will show you much easier ways
of finding the delegate methods.
What is available in there.
In all fairness a lot of the things that we don't know is just simply that when you decide to do something
and you you come across an issue then you think to yourself that there might be a delegate method that
does something like that.
For instance there is another delegate method which I kind of forgot its name but they can show it to
you here.
This is the place to delegate.
It means what happens when something gets pasted into my text field.
Do you want to either know pop a little window saying like the one that you have on safari on iPhone
that says the moment you paste something it says Do you want to search for this or do you want to automatically
run the search.
Well a lot of Obstat they allow you to enter text as an authorization when they send you a S-M mess
with a number.
Then you enter the card.
There's one two three four.
The moment you enter the 12:56 it automatically starts checking for authentication.
So based on that you can do a lot of things and a lot of times you wouldn't exactly know what is in
a delegate.
You have to go through the manual and with the help raise resources read about them and find out you
know what you could do with them or more importantly if you have a situation how you could fix it using
any of these.
So let's keep that up to here and then we'll move on to the next lesson which I think will do the same
thing in Objective-C.