It everybody what's going on this is Caleb began with Deb's slopes dotcom and we're going to continue
building out our app for using protocols to delegate the passing of information between two controllers
so let's pull open our project again.
And in this video we're going to go ahead and do the code related stuff in the last video we basically
just built out the UI and kind of hooked everything up the way that it needs to be.
But now we're going to get into the good stuff the code.
So let's begin actually by creating our protocol remember a protocol is like a blueprint.
It's like a set of instructions or requirements needed to achieve a certain kind of functionality.
So to begin we're going to go ahead and right click on color magic click new file and we're going to
create a new Swift file.
OK.
Now for this kind of protocol where we are dealing with specific functionality in an application we're
going to actually call it a delegate.
Not a protocol because the protocol itself is acting as a delegate between the two controllers.
OK.
It's delegating those tasks so we're going to go ahead and call this color transfer because that's that's
its job.
That's what it's doing.
And it is the color transfer delegate.
That's what we're going to name and color transfer delegate click Create and we now have a color transfer
delegate swift file.
Now of course we're going to need to actually create the protocol right here by typing protocol just
like we did in the previous videos.
And its name is color transfer delegate.
Very easy.
Now we need to think we need to set up a function that's going to do two things.
This function is going to basically pass a color from the button to the view in the first view controller.
And it's also going to pass a label it's kind of past the name of the color on the button.
So we need a function that can accept two parameters one of color and one of string.
So we're going to go ahead and just write the function here for our protocol called phunk user did choose
and we're going to say the user did choose a color of type color and that color is going to be with
a name and we'll we'll use an internal parameter here called Color name of type string.
Can you'll see why I'm using this internal parameter later but it's giving us an issue.
It says use of undeclared type color.
The reason for that we're only importing foundation color is a part of it.
So beneath Foundation go had an import UI kit so we can have both.
And you'll notice our air will go away.
So here is our color transfer delegate and I'm going to explain how this is used in just a little bit.
OK.
So we need to think about what we are doing here when we go into our main storyboard.
We are tapping our plus button and it's loading up our view controller here with all of our options
in our color picker Visi when we choose a button.
It gives us the ability to pull out the title label as well as the background color.
So here's what we need to do.
We need to basically set up a variable in color picker Visi for our delegate.
Okay our color transfer delegate.
So I'm going to create a variable for that and I'll explain this in just a moment but we're going to
go ahead and type of var delegate delegate and that's going to be of type color transfer delegate.
Now the thing is this delegate it says we don't have any initializers.
And so what we need to do is we need to initialize it from the get go.
But at the beginning we're not going to actually have a delegate.
It's going to be equal to nil from the beginning.
So the delegate is going to be equal to nil.
Now basically why we are creating a delegate here is because when we tap on a button we're going to
be able to use this function delegate dot user delegate dot
delegate does delegate delegate user did choose color and we can pass in a color and we can pass in
a name.
Now we can't because the delegate is nil from the beginning and that's where we're going to fix that
and we're going to fix this right now.
So we have a delegate property color picker Visi has a delegate property.
And what we need to do is we need to instantiate that property when we tap on the plus button to open
up our view controller so to do that we're going to use a function called prepare for Segway just like
that.
Now preparing for a Segway basically means when I pressed the plus button I need to tell it what it
should do in order to present the next view controller.
Now if I click on the Segway here that's that arrow represents the Segway.
And if I go into the attributes inspector you can see there is some information for this Segway.
The most important thing we need to think about right now is the identifier and that is what's going
to set this Segway apart from all other segues So let's just go ahead and let's name this present color
picker Visi press enter and now this Segway has a specific identifier so we can get that exact Segway
and do something with it.
So go back to color presenter Visi and prepare for Segway.
This is where we're going to actually present our color visi.
We're going to do this programmatically.
But for good reason.
Let me show you why.
So go ahead and let's just say if Segway meaning if one of the segues and our storyboard identifier
is equal to present color picker the C K that's our identifier.
If it's equal to that we're going to present our color picker visi.
So to do that let's go ahead and let's create a constant that's going to be of type color picker visi.
And then after that we're going to do something with the protocol that that explains why we created
that delegate variable on color picker B.c.
So go ahead and type.
Let color picker Visi and we're going to say that this is equal to the segue dot destination.
And do you notice that it says it's of type you view controller.
That is because every Segway has a destination View Controller.
And we're going to actually set it to be equal to color picker visi.
But if we want it to be of type color picker Visi we actually need to force cast this as color picker
Visi like so.
But since we're using guard let We need to provide an instance in case this doesn't work.
So we're going to type else.
And if we don't get a color picker Visi we're just going to return and break out of this function so
we don't cause a crash.
So guard let is basically a safe way to create a constant that is non optional.
And if it is nil if it does return nil we're just going to go ahead and return out of the function so
we don't have a crash.
Pretty cool.
So we now have a color picker Visi and this is where the coolest part happens.
So like we have said our color picker Visi needs a delegate but it's currently nil.
OK.
Now the delegate is basically going to be used to pass information from one view controller to the next.
So in our color presenter we see what we're going to do is we're going to go ahead and say color picker
Visi dot delegate.
OK remember it has that property although it's not letting us access it.
Let's try that again.
Color picker Visi dot delegate.
We're getting an error.
Let's try to build and see what the error is
initialiser for conditional binding.
Oh.
Since we're in a guard let We need to go ahead and bind that conditionally.
That is our problem.
And now there we go now that we have a color picker we can go ahead and type dot delegate that's a property
of color picker B C and we need to set it to be equal to something right.
We need to basically pin the delegate to a certain view controller saying where are we going to delegate
this information from the previous view controller.
Now if I were to go in and if I was going to set this view controller color presenter AVC to be equal
to the delegate.
Watch what happens when I build and run this can not assign whips can not assign value of type color
presenter AVC to type color transfer delegate.
Do you know why we have not conformed to our color transfer delegate protocol so to do that.
Go ahead and type a comma type color transfer delegate and now we have conformed.
But watch what happens.
We're going to get an error.
And it says we do not conform to protocol color transfer delegate.
Why is that.
If I go look you can see there is a required function.
So we need to basically call that function in whatever view controller we are conforming to that delegate
for.
So get rid of the boilerplate code there and beneath feuded load we need to type user did choose color
from our color transfer delegate.
That's really cool.
Now think about this.
This function is going to be called from the other view controller.
OK.
Let that sink in for a second.
I am loading the color transfer Visi Let me show you on the simulator here really quick.
When I open this up and I click on this what I'm doing is I'm setting the previous view controller to
be the delegate.
And that means that if I am in color picker Visi Now if I press a button I can basically use my delegate
which is now not nil and I can call delegate Dom user did choose color so I can call like this delegate
user did choose you eye color and I could pass.
Well let's think the sender is the button and I could send the background color.
OK there's a color and the name.
I already printed it out.
Sender title label dot text so I could cut that and paste it here.
Now that I have a delegate I should be able to send that information back.
So if the delegate is equal to color presenter AVC It's like I'm calling that function.
On the other controller.
But we need to think what is user did choose color with name going to do in this view controller.
Uncolored presenter see this function right here user did choose.
It should change the background color of this view and it should change this text in this label to be
the colors name.
So go ahead and we're going to say self view.
Well you know what.
I don't even need to call self I can just say View background color equals color and I can say color
name label color and name label dot text is equal to color name.
And that's why I use that internal parameter there because using with name as the parameter doesn't
really make sense but color name does.
So when this function gets called uncolored picker Visi and I pass in the buttons background color and
the buttons title label.
It's going to go ahead and send that through to this function.
Set the view of color presenter VC to be whatever color the button was and it'll set the labels text
property to be equal to whatever the buttons title label was.
Now I am getting some errors here and it's saying that we do not conform to the protocol.
Now if I build and run you'll see that error goes away.
OK sometimes you need to do that but we are getting an issue here and it's saying that background color
is not unwrapped.
We need to do that because those properties do come in as optional because you don't have to set a background
color as well as our title label here.
But you know what.
Since I know for sure that I have a value.
I'm going to go ahead and force on RAP the title label because each button has one for sure.
You wouldn't normally do that.
You might want to actually cast this in some type of guard but instead of using guard let.
I'm just going to go ahead and force unwrap the title label property.
But now let's go ahead and let's just think about what we're doing here.
We are able to go in we can select this button and we set the delegate to have a value right.
We set color transfer delegate to be equal to the previous view controller.
Then once it has a value when we select a button we should be able to pass that value back to the view
controller.
OK delegate user to choose we pass the color back but it's not going to do us much good if we stay on
this view controller right.
That's going to be a problem.
So what we can do is in order to actually get back to the previous view controller we can call self
navigation control or remember we embedded in a navigation controller so that's automatically a property
and all we need to do is type pop view controller and that's going to pop us back one view controller
of course we want that to be animated.
And now when I select the color button it should pass the value it should.
Sorry it should pass the value for the color for the title label and it should pop the color of your
controller back to the direction it came.
And when we go back to color presenter AVC this function is called.
So we're going to set the background color and the color name label to have a value.
Do you want to try it out.
I do.
Let's build and run.
Let's go see how we did and we should be able to call this video Arap.
So check it out.
If I select here it opens it up.
We don't get a crash that's good.
And now when I select a button it should set the view and the label to match.
Check it out.
It works blue green red purple.
So so cool.
So this is just one amazing way that we can basically use a delegate to transfer information and communicate
between two view controllers.
Super super cool.
Now of course there's one thing we should do this delegate is optional.
So what if for some reason the delegate was not able to be passed in we would get a crash.
So we should go ahead and type if delegate is not equal to nil.
That should be an equal sign if the delegate is not nil then we should call a user to choose.
And we should pop the view controller back.
OK.
That's just the way to be safe and to make sure that we are not going to have a crash.
Now of course that's not going to do anything right now because our delegate is non nil.
We pass it in when we create the view controller.
But check it out guys.
This works were able to pass colors and all kinds of other cool values if we wanted to.
This is super super awesome.
So great job using your first protocol to use the delegate method for transferring data between two
view controllers.
Awesome work in the next video.
I'm going to show you how we can use what are called mutating functions to toggle on and toggle off
certain switches.
It's just one particular use case but it's pretty useful and you could use it in a lot of different
ways so I'll see in the next video.
Awesome job with this one.