In business and we're going to see an actual delegation happening in a real US application what we are
going to do is this we are going to go in Iowa with Tuvia controllers from what you can do or navigate
to the next one in the next one.
We will have a text field.
We enter a value and we hit the back button but when we come back for the very first view controller
we will see the masses we entered in the sack and The View Controller is essentially going to be a way
for us to send a message from second to regular First the controller then the back.
Why then is being pressed so let's see how are we going to do that.
And when I go to school I'm going to start a new project this new project is going to be I was single
view applications swift and I'm going to call it delegation.
I guess I my delegation I was going to do some stuff that we already know how to do them like you know
adding in new View Controller and everything.
So let's say View Controller is our very first controller and I'm going to have myself a second view
controller.
I'm going to call this one our let's call it green view controller because I'm going to make it's by
gone green obviously.
So in the storyboard I have the default one which is essentially the The View control outlet.
I'm going to have another one and this is a good time for us to you know repeat some of the older topics
things that we covered in the previous sections.
This is the one that's going to be green so I'm going to make it's going to become some sort of figurine
and it's in that identity in a spectrum.
But I call it to be the green View Controller.
So so far we have to view controllers.
I'm of course going to have a segue between this one too that one of the type Schull.
Well I'm going to call this fun Green segue.
Green segue Heidi.
OK.
So we've got all of these taken care of.
Well next thing is I need a button to navigate to the green View Controller.
So I'm going to add a button and this button since it's going to take us to the green one is going to
be green.
And it says in maybe Black says gold or green and I'm not going to make a Segway for it in here.
We're going to make it programmatically.
And I'm also going to have a label in here which is essentially the message that we receive.
All right.
So this one I'm going to say actually it's my gun.
I'm going to leave it as dark grey by this text.
I'm going to make it daring because this is the message we received from the other side.
And currently it says message.
Is there something that I want perfect.
So we got that now in the other of your control.
I'm going to have to have a pardon.
And that's what it is up here.
And that button says with a white background that is you know indicative of the view that is going to
go back to.
It says Go back and that's about that.
It says Go back in black and then I have a text field that I can enter a value in it.
So that's a text with my text which is a little bit larger than I like it without borders.
I like it to be a little bit larger.
I can say it's back.
These are all very very light gray and text and it is there's a message perfect of what everything they
need in terms of the outlets.
Now I have to connect them to their respective of things in the coding area to the outlets as well as
our actions.
So in here on when our Lord my assistant editor I'm going to say for the first few controller I'm going
to need these outlets I need.
And an action that says action go to Green action.
And I also need a label that says receive the message label.
And that's about that.
I've got the label and I got an action that I'm going to go to the other one to the green one and the
green one and they have to do some cleanup.
I'm going to do all of that right now.
And in the green one I to have an A go back action.
So that's going to be a go bag action.
You know as always and I also have a text field that says not sage text.
So I've got a text with a body action.
Everything is ready.
So now let's go ahead and see how we want to do how we can pass that on.
The first thing I want to do is this every time that I press the button in they go to Green action.
I want to say this I want to say self guard perform saying they identifier and here is the identifier
that I need a thing I called it green.
Identifier.
Yes.
Green segue.
So that's the name for it green segue ID understand.
Isn't it.
Now here's one thing we did that when we go to the game in action we performed the Segway.
If you recall we also have to prepare for that signee and he has hongbin do that.
And when I say prepare for a Segway and in an ideal world if you want to be super careful about everything
you have to say if Segway DOD identifier is blah blah blah.
I don't really I'm not going to do that simply because I know I only have one Segway.
So the only thing I'm going to do is that I'm going to say that I'm going to call it green in Staats
is going to be sick of a destination.
Now keep in mind the destination is a you view controller but they want it to be as green if you can
it.
I want it to be of this type.
I want it to be forced as you know inferred as a green view controller.
So so far be prepared even though we are not doing anything in here just yet and we perform the same
way.
Let's see what we see in the green view controller in here.
We have a text field in here that we haven't done anything and we have a function that takes us back.
This first thing with the function that takes us about it says self that this is true and that's about
that.
And don't do anything at completion.
So that's one part of it and another thing I'm going to do is I'm going to say let's text to send.
Is going to be message text field that dot text.
So let's get the text in the message text.
We don't we have to do something with it.
We have to do something.
I just don't know what that thing is.
So if all goes well by now we should be able to navigate back and forth.
So let's go ahead and run this application even though some variables are not being used just yet they're
just sitting there on their own for not doing anything with them but this application essentially does
work and takes us from one of your contorted to the other one where there is a text field that we can
enter values and where we can hit the back within.
But obviously nothing gets communicated between our two controllers.
So once I have my eye out I'm going to call this file to be a protocol create I know I have this protocol
that I'm going to say it is actually a protocol it's called sending a message delegates and that's about
that it of one method called function send me which essentially gets a message of to tie a string and
that's about that's all it does.
So here in my view control the very first view controller I'm going to say this class view controller
conforms to you eye view controller and also sending the message delegate.
So it conforms to both of them.
And now you get an error saying well if you want to conform to this you also have to give it that method
that method is called.
I forgot this cause I'm just going to add it in here.
It says send a message.
So it has to conform to this one.
So what do you want to do whenever this function happens will come to this in a second.
The other thing I want to do is this I want to go to my grave you can sort of just like the last time
this is the guy who is going to be our sender.
If you remember from our previous example this was for instance or a user.
So I'm going to say well I have a valuable I'm going to call it the elegant which is of the type sending
the message delegate.
So I have this variable in green view controllers.
So what do you want to do you want them to do this will they want them to do.
When I pressed the go back action I want my delegate to call its message and I want to send this particular
message text to send.
So that's the text that I want to send through the delicate matter.
Now who is going to receive this.
On the other end The View Controller.
This function has to receive it.
What should it do was it received it.
It has to put it in the received message Nevel the text as to be become message.
Now there is one piece still missing and that is this function works well if we reset giving a message
it puts it in our label.
This one works well.
It sends it to its delegate's.
The only thing we don't know is the green view controller is delegating his tasks to which object.
And that's something they haven't done yet.
So if I go to my view controller and in here I'm gonna say green stance
that delegate is going to be self.
What it means is this it means the green stance is going to deligated tasks all of its tasks to self
which is the view controller which does conform to send the message delegate.
Which means if the any green stands delegate's its particular function call to send this class viewer
response.
So when the game view controller says in case you were going back there to get that send this message
the one who is going to actually receive this message is going to be our view controller.
This is because our warning is because we didn't change anything and well turn that into a constant
and let's run our application one more time and see what do we get.
And here's my simulator and I have go to Green and saying hello white go back I says hello once I go
in here.
I say Are you a fool sky.
I don't know why I came up with that.
I go back and it says beautiful sky.
It sends a message to the other side.
Let me quickly recap things that they made a big chunk of it doesn't really matter.
So our view controller has a label has an action none of them Mandos performs a Segway.
We know how that works.
This part is a new thing.
Prepare for Segway.
It says get the Segway destination as Glenview controller and set its delegates to be here.
Now Grangeville a controller has a delegate valuable in it which is of the type sending message delegate
and seen sending message delegate also has that particular function whenever we call that one delegate
to send somebody would be on the other side trying to receive these.
In this case that it has to be the view controller the sky which does not conform to sending message
and get either receives that one.
So that's about that.
We managed to successfully communicate between two objects of our differing classes.
One of them conforming the other one using delegate valuable and let's move on doing the same thing
once again as an exercise I will also do it at the end.
In Objective-C.