Are you struggling with protocols and delegates in iOS? Keep on watching to
learn how you can build out this application really, really fast and easy.
Alex here from rebeloper.com and if you want to build out this application go
ahead and hit that subscribe button, open up Xcode and let's just jump right into it.
So let's take a look at our example project that I have built over here.
Let's just a build and run and see what we are going to achieve today with the
help of protocols of course. So while you are waiting for this to build go ahead
and check out the link in the description to get the resources for
this tutorial. So here is our Apple logo with some text and info and this is
basically a profile so you can get a hands-on experience with protocols, okay.
Let's take a look at our settings because in the settings we are going to
change some things and that will be reflected in the profile view controller
and let's see let's select our swift logo here and some red and there's some
name. That's Swift and language there we go and hit the checkmark and there we go.
Everything has been changed and all of
this with the magic of protocols, okay. Let's just start building out this app
from scratch. So I will create a new Xcode project here and it will be a
single view application. So file new let me just resize this a little bit so we
can see it better. So a single view application let's click on next here and
I will name it protocol delegate, okay. And make sure that the user interface is
storyboard so no SwiftUI. They aren't any protocols own delegates in Swift UI.
Those are stats and observables that we are going to use and in this
instance protocol delegate is okay storyboard let's click on next and let's
just drag and drop it anywhere in our computer. Let's click on next and there we go.
Let's just resize this so we can see
this a lot better, great. Now what we want to do is take a look at the downloaded
resources now and inside here we have some assets profile, profile view controllers,
scene delegate. So let me just delete what we don't need here and just drag and drop
what those will be. So let's just click on delete here, move to trash and let's
just select all of these and drag and drop them right over here, okay. Make sure
that we copy all of them accordingly, okay. Next up I want to delete the
main dot storyboard because I don't need that I don't really like to work in
storyboards as you might already know. So let's delete that one final thing is to
well there are two things that we need to do here. First of all let's just
delete the main interface. So there we go. Let's leave that as empty and under info
just select application scene manifest, scene configuration, application session
role, item 0 and delete basically the storyboard name. So for that to be shown
let's just, let's see this more, okay. Let's go back to our project and now let's
just take a brief look at it and how it looks like. Well in the scene delegate we
are basically creating our profile view controller and we are showing it. In the
settings view controller we are just setting things up. Go ahead and take a
look at this in that I don't want to waste much time here that's just a
building run and take a look at it in the simulator and yeah we have a profile
model here and we some assets icons, white icons that we
are using. So let's see here. Here we go. Let's select some other stuff but
nothing really will happen, okay. The code is running so now it's time to talk
about the delegates. So what we want to understand here is how delegates work.
Think of a client or a worker and the boss. Now the boss is always telling the
worker what to do and the worker is listening to its boss. That is kind of
how protocol delegates work. Now in our case the settings view controller is the boss.
We are setting things up so we know that we really want to change the icon.
So the boss is telling the other view controller the worker, the profile view
controller, okay I want you to change this and this. The icon, the title and the
info, okay. So think of it like this. The settings view controller is our boss and
the profile view controller is our worker, our listener for that delegate.
So let's start off with the settings view controller and to basically create our
boss that is a protocol. So let's just go ahead and create our protocol here and I
usually name it the class so the settings view controller and I append
the delegate to it. So delegate, okay. Now this is basically the boss.
Now what can the boss tell to our workers and this is where our functions come in.
So func and let's say in our case is just pretty straightforward
did change profile and we are going to say okay we did change profile to this
profile. So we are going to insert our profile here. So did change and
let's just use this underscore here profile
and it will be of type profile, okay. Again profile is a model that I have
created and I'm using it here. So now that the boss is ready we want to
basically implement it on our settings view controller and that is again pretty
straightforward just create a delegate. So var delegate and you could name it
like settings view controller delegate it doesn't really matter in some cases
if you are adhering to a lot of protocols then you might want to change
this to a more specific delegate, well the variable name, okay. Delegate and
that is settings view controller delegate and this is an optional.
You might use it, you might not. But we will use it and we are going to use it when
we actually tap on that checkmark at the top right and that is a UIBarBottom
item that has a selector and basically what should that do when we are tapping
on it and it's currently printing out the profile and in the previous build
you can see that we have no icon one this is what we tapped on and what I'm
going to do here is call the delegate. So delegate dot did change profile and I'm
just going to add in our profile, okay. Now this means that whomever, well the
boss is giving this command did change profile. Now whomever is listening will
perform and this did change profile so it will basically grab the profile that
I am feeding it in here. So let's listen for this protocol and let's go to our
profile view controller because that is the worker that we are going to listen
in and you could add adhere to the protocol like so like settings view
controller delegate and then you add the required functions but as you can see it
already says that we need to add our protocol staffs but that is kind of messy
and I like you just create some extensions on our
viewcontroller. So ket just scroll all the way down and create our extension here.
Now let just wait for the fix and just tap on the error and just fix it and
Xcode automatically adds that protocol staff, okay.
Did change profile. Now we can use our profile that we have fed in the
settings you controller to our advantage. And what do we want to do? Well we want
to present it on this view controller and neatly we do have already function
here apply profile. Now this adds the image view, the icon image, the name label
and the info label according to the profile that we feed into this apply
function. And just a quick note here because we want to have tint color we
have set the UIImage to the rendering mode of always template, okay. So this is
pretty straightforward apply and we are using our profile, okay. Now we did
conform to the protocol delegate but we are still not listening to it, okay.
And that is a pretty straightforward to do when we do create our settings view
controller we want to set the delegate to this view controller. So when we did
create our controller it's here. We just grab our created controller and that
delegate should be our view controller. The profile view controller and that is
self, okay. And that's it. That is how you can add the protocol delegate to your
projects. So let's just build and run again we are a kind of a master and
worker this is the worker the profile currently we have some default values.
Go ahead and take a look at the code for the details and now we go to the master
the boss and we select let's say our rebeloper logo.
Some nice color to it and enter rebeloper and let's enter some info
dev and when we do tap on this check mark whomever is listening for this
delegate let's say function it will implement it. So yeah the profile will
listen to it and there we go we have implemented the protocol to our profile
view controller. That's how simple it is. I hope you really enjoyed this
foundational video about protocols and delegates. Now I want you to hit that
notification bell because in my next video we are going to talk about Apple
sign-in with firebase, really awesome stuff. While you wait for that make sure
to check out also these two videos as well. I talked a lot about SwiftUI,
iOS developer in general. So make sure to like those too and as usual I will see
you in the next one.