So will come back again in this exercise you are supposed to rewrite the code we did in our previous
lesson which was swift and redo it again you know Objective C.
Now this is one of those exercises that they really encourage you to attempt it on your own simply because
doing delegation even though in terms of the concept is exactly the same thing.
Blade has some of the stuff that you know you may not really grasp in the very first attempt and doing
them once in Objective C is going to show you some you know some more insights into using them and coding
it.
So I really encourage you to actually try it on your own.
I'm going to pause the video now for a few seconds.
This is the time for you to pause entirely and try it on your own and then come back to the code and
the solution that I have and show to compare it with what you have the.
So let's get started.
In the interest of saving time I've actually made parts of my application already I've made my interface
builder the Baden's I've made my segue.
I have connected all these different you know outlets.
So in my view controller I have a label received message but I have a call to action in my view contorted
and I have to program the body of this method in my view controller.
I have the textfield and I have a go back action.
And of course I have to program the body of this one.
So let's first get the things we know out of the way.
In my view toward that end I'm going to go ahead and say import the green of your controller and you
might go to with green action.
I'm going to say self performs Segway performs Segway with identifier on the identifier.
I gave it was called Green Segway ID.
I have no senders.
So that's one part of the problem.
The other part obviously is the preparation some going to say void prepare for Segway.
And again you could check for the sake of the identifier or you could skip it just like what I'm doing.
I'm going to say green View Controller Green an instance is going to be Segway our destination view
controller and that's about that.
So so far we managed to prepare our getting in stance and we also got our Segway to be performed.
Now I'm going to go to Green view controller on the go back action.
I'm going to get emesis drink let's say text or send that is going to be the name of my textfield the
DOT to text.
Obviously the value in it I'm going to store it and then I'm going to say self this mystery controller
animated.
True.
And I don't want to do anything.
It's completion.
Perfect.
So we got this far and by now we should be able to launch an application.
It should be you know managed to go from one viewpoint or to the other one and do the basic stuff just
the way that we expected it in here.
I could go ahead and say go to Green go perfect.
So the next thing we need to do is we need to add the protocol to our application so to do that we can
either introduce your protocol inside the body of one of your data files or you could do it the way
I personally like to do and that's adding in new data h find someone to say command and here is a header
file and this other file I'm going to call it put a can remember protocols.
They don't have any logic so all they have is actually the body of their declaration and because their
declaration they are like interfaces so they are data aitches header files.
So I'm actually going to say import and I have to import the foundations simply because without the
foundation there isn't really much we can do.
The next thing is say at protocol and my protocol is going to be called sending mass sage protocol and
this thing that you see in here it essentially means that the sending masses for the core conforms to
this object itself.
This is actually an optional thing.
You could if you wanted to remove it and the only method I have in it is this voice.
And it sends a just call send and the send is actually a and has a string on that and assisting is called
message just like what we had last time.
Now as you might very well remember from our swift example one of these two view controllers view controller
that h n m not degree in one.
They have to conform to that protocol.
And the other one should use the protocol.
So first this can conform to it.
And when I was at that stage I'm going to say let's first import whether re-importing protocol import
protocol.
And then my view controller is a subclass of viewpoint.
And it also conforms to sending message protocol.
Now that I have it conforming to a sending message protocol I have to be including those methods which
is essentially this one in the body of my implementation which is in my data and so it might that ominous
one is going to give me an error in itself.
I have to add that when I go in here and say well this one is in here.
So whenever somebody calls send from the delegates who are delegating to you being called the view controller
this function should run and if this function does run I'm going to say received Laymon that text should
become whatever we receive just like the last time that we had done that.
Now the other thing that we had to do was we had to go through Grandview contorted and say hey this
one is using the Send the message protocol as a very old outcome but to say at property and that property
is going to be non atomic and is going to be of the type i.e. the Veach does conform to what does it
conform to sending message protocol which I have to import it.
I'm going to say import protocols and it is going to be sending Nassir protocol and I'm calling delegates.
So what is this.
This line is essentially the one that we had in the swift which was our delegate of the times sending
message delegate that was that and we have it in here as a property which is this what exactly.
And that property since we don't know its type we're going up with doesn't the objective.
Are saying that this is an idea which does conform to this.
It is called of course delegate and it is non-atomic.
We'll talk more about the atomic non-atomic and retain and sign at the end of this section.
So keep this one for now the way it is now.
I have that property.
And the next thing I could do is I could go to my viewpoint or that am saying when you make the union
stance do you agree any stance that delegates to self so delegate tasks from this one to here.
Here being my view controller.
And finally in my viewpoint for that am I'm going to go ahead and say every time you're going back do
self delegate send what you want to send.
I want to send this particular text.
This is exactly identical to what we did for swithe is exactly the same thing but the return objective
c when we're going back we get the delegate and we use its method.
We send the something doesn't matter what that thing is.
It is the one in the text which you know View Controller Vicki form to this particular protocol which
means it's methods most notably this one should be activated in here it should have a function body
in here the body of it is get the message up within the label.
And finally in here we are saying that when you're preparing for say what you want to do is you want
to say this new one the new instance then it gets to here being the View Controller.
So I'll go around this and if all goes well go to Green Hello from green.
And it says hello from doing.
And that's about that.
Using protocols and delegation patterns in both the iOS 4 of its swift and I was with Objective-C.
Let's keep that in mind in the next lesson.
We're going to move into something that is using an existing delegation from one of four athletes.