Welcome back again.
This lesson is actually an exercise on what you're supposed to be doing is you have to make a shared
entry form.
So essentially you have one view controller that has a little text field in it and you can enter a value
on it.
And this view controller is shared as a child of your controller in two different view controllers.
So let's say you have one view controller that because the first view controller and the other one that
because the next view controller and there is something in between them that is the shared the view
controller and the shared one has a text field in it.
Now the challenge that you have to solve is this every time that you enter something on this text field
and you had enter or the text ends we have to get the value from that text field and represented in
any view controller that we currently are at.
So you might be in the first few controller or you might be in the next view controller in each of them
you should be able to get the value from that other shared the view controller to do that.
I'm going to give you a little hint that you must use a delegate.
So that's like the best approach for doing something like that.
You use a delegate you make your own protocol and you make a delegate that delegates a task from the
shared the view controller to either of the two of you controllers that you are at right now.
So it takes some time.
I try it on your own.
I hope you know you succeed in it or if you have questions ask me otherwise I'm going to get this started
on this in just a few seconds as well.
If you want you can pause the video and try it on your own and then watch me doing
okay then.
So I'm going to get this started as well.
I'm going to go to court to start a new application called shared entry form app on my shirt and a phone
only has one value that we can enter.
But that essentially shows us what we should be doing.
And when I go to my main a storyboard and my main storyboard.
I'm going to add a new view controller.
So that's my next view controller.
And for my next you can control I'm actually going to make a new file or new Coke with each class and
I'm going to call this one next view controller simply because I have the first one.
The main one which is here on the left lets me go back to the storyboard which is here and I have the
next one.
So for the next I'm going to go in here and say it's class is the next view controller which should
autocomplete but doesn't for some reason.
Let me try again.
Yes.
Next view controller stop.
One of them is this first one.
One of them is the next one.
And actually the next one umber changes color to be a little different maybe like that.
Now between this one and that one we need a system of going through them.
So I'm going to go ahead and say from this one.
Let's go to the other one.
And that is by adding a button.
So I'm going to say by then and that but then I'm going to say it's here.
It has a background color which is similar and says go to next.
Now because I'm simulating on iPhone access Max I'm actually going to change my view to the same as
well.
So I could actually see exactly what I will be seeing and I don't need to set any kind of strains or
things of the sort.
So what changed that.
I'm going to place this one around here.
Great.
Now I have the two view controllers and then I want to have a v to go from one to the other one.
So I'm going to say from the go to next do this do a Segway show.
So now it goes for the green one and then for them when I do it I'm going to say I have a container
you.
So let's go ahead and add that I'm going to come in here and say container review this container of
you I'm going to add it to my first view controller.
Let's say this one about that I have made me this little you know container view controller.
So I need to add this container of you can't order to my dream view controller as well.
So what I'm going to do is this I'm going I actually copy this UI if you go in here and paste it.
The reason I copy paste is because I wanted to get the same size.
Otherwise there's no real reason for it.
Then I'm going to go ahead and say this one too.
There it has a Segway and that Segway is called embed.
So it essentially embeds one within the other one.
This is just like how this one is happening.
If you select the Segway in here and you go up there it says the storyboard embed Segway and that's
what they did in here.
I manually made that let me redo that once again from my UI view which has the same size of this one.
I'm going to control a drag over the view controller and say Segway embed which means it works as a
container review for this too.
So now this one is shared between the two of them.
And the proof of that is if I have a text field and I place it somewhere like here.
I changed my color so I can see things a little bit easier.
So that's the color.
And here is my text with a little bit larger.
And I'm gonna say it has a place holder saying text and I'm going to make it a little larger.
Something like that with a different background color perhaps something like that.
So if you have this now and if we run this application we should be able to see the same view controller
embedded in here in our main view controller as well as in the green view controller.
And if that works it means we are one step closer to what we want to do.
So now we have that text field is there.
It works.
I can actually enter values on it.
Obviously I'm not going to be able to fetch these values just yet but if I go to the next a gain I have
the same thing and I can see the values in here.
So the next part of this challenge was this every time I type something and I had to enter I want this
value to be shown somewhere in this view controller.
So the first thing is I'm going to add a little label to both of my view controllers.
I'm going to say I have a label that Labor is going to be here.
That value is going to be like that and maybe a little bit larger a different background color so we
can actually seat.
I don't want to copy that paste it in here as well because we want to be able to see the same thing
in two different places.
Now I need to add some programmatic matters.
So the first thing is this I'm going to add a new view controller for the share of the one I'm going
to say command and this is going to be shared the view controller.
So that's my shared view controller and I'm going to come back to my main story board say this is actually
the class of it is a shared view controller.
Now I have that one taken care of.
Then I'm going to head to the assistant window and in here I'm going to say for my view controller.
Here are the things that I want.
That's the main view controller obviously.
I want this label.
So that's going to be called Main label.
And then I'm going to go ahead and select the other one.
The next view controller I'm actually going to do the exact same thing I'm going to say in the next
one.
There's also someone called NEC a main label so main labor.
So I have the two labels taking care of then the next thing I need is with this guy.
They share the view controllers in the shared view controller.
I'm going to go ahead and say there is a value in here which is the text with.
So that is going to be my or Maine again.
Text field because they're all separate.
Each of them has its own class.
It doesn't really matter what you name them as long as you know what your naming them.
So let's go ahead and you know change some of stuff in here.
The first thing we have to do is this.
I'm going for a shared view controller class and take care of the text field delegation.
So I'm going to go to a shared view controller and in here I'm going to get rid of all of these and
I'm going to do these things to say you a view controller.
There's this superclass of the shared view controller all saw you I text field delegate is a protocol
that we conform to and since we're conforming to that I can get should return text field should return.
And every time that the text file should return here the things that they want to happen I want the
text field to resign its first responder.
I also want to get the value from the text field and show it in the order guys label the label that
is in the order of view controllers.
So that is something that we have to deal with using a protocol and a delegation.
So for now I'm going to say just return true.
So that is happening now.
The last thing I have to do in here before I change this I'm going to say main text field in my view
did Lord dot delegate to here.
So it does delegate in yet understands when we had to enter by then let's try that and make sure that
that's actually working.
I'm going to go in here and my application and once it loads where they want to do it I want to launch
the keyboard and I want to see that it does that dismiss and that's going to give me the confidence
that I need.
So that's going to be the keyboard.
I'm gonna say hi and hit enter and the keyboard disappears.
So that part is working.
Now it's a matter of sending this message to that label or if you were in the other viewpoint or whatever
the message is in here into that label.
So for that we need a protocol.
So let me go ahead and say I have a newer swift fine and I'm going to call it shared shared view controller
protocol or you could just simply call it protocol but since you're you know in the future and you make
larger applications you need to know exactly which one it is.
So in here in my new file I'm going to say I have a protocol and that is protocol is essentially called
Shared View Controller delegates.
Now the reason I call it delegate is because that's a very common practice.
You don't have to call it delegate but if you remember this one it's called you a text field delegate.
The other one was called you a picture of you delegate is a very common practice.
And my delegation has only one function that function says fetch text and the text has no argument as
a parameter that is a key value which is a string.
And of course the functions inside protocols they don't have a body.
They just have a declaration.
So does my protocol.
Has only one function.
Now how are we going to use this.
I'm going to go into my shared view controller and see my share the view controller has a value event
called delegate which is of the type share to view controller delegate.
So this is the delegate from the same type and we can call it whenever we want it.
So when do we want to call it.
You want to call the delegation every time the text field returns so I want to say delegate DOT and
the only function that it has is important to me is fetch text and it's got the same value you're trying
to fetch takes.
What is the text.
The text is text field not text value.
So what they have done here is this.
I've said there is a delegation method that features text and sends it to another delegate.
This guy shared the view delegate whoever conforms to here.
Now nobody so far conforms to here so far.
We just send the message to our delegation so let's go ahead and find someone who actually conforms
to here and those are these guys.
Their view controller and the next view controller.
They both have to conform to this.
Some were go to view controller and say you my view controller shared view controller delegate is a
protocol in here now it should give me an error saying that hey you have to implement that function
and that's exactly what they want.
I want this function to be in here and here's what I want to happen in it.
Every time that this function sends me a text I want to put that text in my label so I'm going to say
main label dot text becomes whatever you got me.
And that's all I have to do in here.
You actually after the exact same thing for the next view controller the memory format is in here so
it's a bit tidier and cleaner so we don't have a lot of code going all around.
So that's that and since I'm being very lazy I'm actually going to copy everything just like this to
the other sites.
I'm going to say all of these copy go to a next view controller and say paste everything for the next
few controller.
Again I have the main label a gain I'm conforming to share the view controller delegate a gain because
of the Shared Value control that I get.
I have to call this function and everything in this function gets called whatever value it finds we
put in the main label.
Now let's do a quick recap of everything we have done so far and try to understand what we still need
to do.
So we have a shared view controller protocol that this protocol called a delegate is essentially having
one function that the one function gets called every time the shared one the middle one essentially
has its text field should the return called when that is called it finds the value of the text field
and places it within its delegate that delegate.
It's a valuable of the same type.
It's an optional day event and it is not yet initialized.
This is a very important point yet not initialized.
The reason I'm typing this in here is because I want to draw your attention to it that no one has called
this delegation yet and that is the last step in our project.
So we have to do that.
Now let's assume that is working and if that is working every time the delegate fetches a text it pushes
that takes to whoever has initialized that and hopefully that whoever is the view controller which is
gonna show it to us now it becomes a matter of.
Well there is this delegation going to become initialized who is going to be calling this delegation
to do that.
I'm going to go to my viewpoint order and write this.
I say prepare for Segway.
So now you might be wondering why are we preparing for a Segway.
The reason is this you know a storyboard.
We have a Segway going between this view and that view controller.
We have another Segway going between this view the green one and the shared view controller.
There's the skip the one in the middle for now because that's not the one we actually care about.
These two are the ones that we care about them.
So one of them is from the main view controller to the shared.
The other one is from the Green to the shed.
So let's call them the first one.
I'm going to call it white.
Two shared I.D..
So that's a white t shirt idea and this one is the green.
Two shared ideas.
I'm going to paste that and say that actually says Green.
Two shared ideas.
So both of these are essentially saying based on whatever we want to do for them we can do it within
the prepare for a Segway.
So I'm going to come to my view controller say if Segway dot identifier is a data value here is what
I want you to do.
I want you to say let's share V.C. to become Segway.
That destination as.
And I'm going to get it as shared the view controller.
And then I want you to say share.
Do we see that delegate to here.
So this is the first time this delegation is actually being implemented.
This is the first time someone is actually saying well if you made any calls to a function I will respond.
Let's see that a little bit more visually.
If you made a call to a function.
Who is that.
Who.
This is the shared we see if shared we see made a call to a function which is this one fetch text.
Someone has to respond and that someone is the view controller here.
It says if you made the call to that I will respond to it.
I will do something that thing in this case is fetching the value and placing it in a labor.
Now they have that they can essentially copy this entire thing the way it is to my green view controller.
I'm going to say Copy.
You don't always get this like you because obviously I plan this things in advance but for you you might
want to do different things.
But for me and my next view controller.
When I'm preparing for a Segway if that secret identifier was green to shared I.D..
If that was the case then get the shared V.C. to be the same as the shared view controller and delegated
to here and will be delegated to here.
We can call this function.
Now let's go ahead and try our application.
If it all works I should be able to type something in the text field of the shared view controller and
fetch its value.
Show it on my labels.
Let's go ahead in here and say hi.
Let's enter that says Hi.
Let's go to the next one in here.
Say hi hit enter.
That's 1 6.
Hi.
So that was an introduction to container views.
Doing a lot fit delegations as well as protocols.
It wasn't an all round you know application with a lot of things that we have done in the past and we
get a sense of doing a quick review over them.
Let me know if you have any questions and let's move on.