Everyone and welcome back the class Johnny be here with slopes and in the last lesson we did a little
bit of a project set up getting our thoughts and comments classes up to date as well as adding our options
images here.
And we hooked it up in our a fat cell but I forgot to hook it up in the common cell So this guy didn't
do that here.
I went up all option click on the comment cell to bring up our assistant editor and I'm just going to
control and drag into our thought cell here and I'm going to call this Options menu and say connect.
Right now it's good.
All right so let's go and check out our cell does file here.
And what we're going to work on is making it so that we can tap on that Options menu image and have
something happen.
OK.
So first off we're going to need a tap gesture recognizer.
But the other second key part to this is a protocol delegate that we are going to code so that we can
pass information from the thought or comment to sell to its controlling controller.
OK.
So that is what we're going to work on here.
First let's start with just the tap gesture recognizers that's a little bit simpler and let's see here
I'm going to get rid of this old comment that we have here.
We're going to do is we're going to start out here in our configure cell we're going to start out with
the options menu hidden.
So going to start out with a hit and then don't hear me.
Give us some space here.
We are only going to show it and add the gesture recognizer if the person that is logged it in was the
person that made that post.
All right so this is why we need to have the user id part of the thought class because now we can say
if thought that user ID is equal to and then we're just going to get the user id for the person that
is locked in here.
We can say Current User ID.
So if the person that is logged in made this post then we will show the options menus when we say Options
menu is hidden is equal to false.
So it will be shown and then we can add the texture recognizer.
So say the options menu dot is user interaction enabled.
So first we have to make sure that the images user interactive will stay true.
And then they will create the type gesture recognizable say.
Let tappy cool UI UI gesture recognizer and will initialize it and the target is self and the action
is a function that we haven't created yet so it's going to create that real quick with select or function.
We have to expose it to the objective c framework so I'm going to say an objective c funk and say thought
options tapped OK then we'll fill that out here in a second.
But here we can say pound or hash tag selector and then we call that function that we just created thoughts
option tapped.
And then finally we are going to see options menu dot add gesture recognizer and tap.
All right.
There we go.
So run through this again.
The person that is logged in is the same person that created the post.
Then we're going to show the options menu.
Set the interaction enabled equal to true and add the tap gesture recognizer which calls this function
here.
Now we want to be able to if we look at our mockup whenever we click on these option image we want to
be able to put up this alert.
So this alert this is added from the view controller.
And right now we're in the thought cell.
So how do we get that information.
The thought that we have tapped on how do we get that information to the controller.
Well we're going to use the protocol and delegate pattern to protocols and delegates are super useful
for handling functions and passing data around.
And it's pretty easy to implement.
How we got to do is we're going to create our own protocol and we're going to name this one thought
delegate.
OK.
And then all we can do is we can write our own functions that this delegate has to conform to.
So if you look at things like our main Visi these are protocols that the main Visi is conforming to.
And when we add these protocols there are specific methods that we have to conform to such as the table
view.
So for a wide number of rows and section These are methods that are required for the for these protocols
to work.
So we're going to do is we have created our own protocol and we're going to write our own method that
has to.
If a controller or a class conforms to this protocol.
It has to implement these methods.
So we're going to say fat options tapped and then we can provide arguments.
We can say thought of class thought OK.
It's going to save that.
And then if we come over to the I mean see if we implement this protocol which was thought that looked
good then we should get an error here telling us that it needs to implement the here we go.
So maybe this does not conform to the protocol.
And do you want to have protocol stubs if we say fix then it automatically throws this stub in here.
That is our thought options tapped.
All right.
And so here we are going to receive the thought that we could go on over in our thought so once we actually
finish up over here.
So that is pretty cool.
So this is where we create the alert to handle the lesion.
Okay.
Very good.
All right so back into our thought cell that swift How do we finish up here.
Well what we've got to do is first we need to have a delegate very well so when I say Private our delegate
of type Fard and delegate.
And this is going to be an optional and then down here in our configure cell we are going to receive
a delegate of type thought delegate and then here we're going to say self that delegate is equal to
the delegate that is passed into the configure cell function and this delegate is actually going to
be the let's go ahead and add that here in Maine visi.
So in Maine Visi now we need to adjust our.
Let's see here configure cell function right here.
So now we are expecting a delegate.
Oh I need to save our thoughts sell first.
Want to save here.
And then in Maine Visi here this is going to be a delegate and we're going to pass in self.
So the delegate for this is actually the main Visi is going to save that from back into thought so.
So that is all good.
Now lastly all we need to do here is in the thought options tapped we just say delegate it thought options
tapped and then we passed in the thought which is our variable appear that we set when we configure
the cell so now when we click on one of these the the just your recognizer picks it up and calls are
Slichter function here which then sets the delegate calls the delegate function which is picked up here
in our main Visi her right here and the thought option's tapped.
And let's just check this out this real quick.
I'm going to say print dot dot we'll go with a user name.
OK.
And I'll run this and test it out.
All right.
Assume just going to click here and we should see printed out and look like it will work.
So let's go ahead and check out what I missed.
Ok silly me.
This should be a UI tap gesture recognize her name.
We go and try that again.
All right it's going to come and is serious.
And the first thing that you'll notice is that I have some comments here that were added by a different
account than the one that I am currently logged in with.
And you'll see that those do not have the options image showing.
So our check here we're checking the thought Don't use your I.D. against the currently logged in user
ID is working.
So that's good news.
And then if we click here then sure enough we do see printed out down here the user name.
So that is good.
All right so here in Maine we see where we are printing this out.
This is where we are going to work on the alert to handle deleting thoughts here and here in the future
but right now let's go ahead and repeat what we just did for the comment to sell.
All right.
So jumping into the comment to sell I'm going to give us some space down here.
Close this bottom pain and just like we did in the thought sell I'm going to start out here in the configure
sell by setting the options menu that is hidden equal to true and those go ahead and do our check.
So we're going to say if the comment that user ID is equal to the path.
No I think we need to import firebase here so import firebase.
All right.
Here we go.
So off and off that current user that you ID.
So if the currently logged in user is the owner of the commande then we will say Options menu that is
hidden is equal to false.
And then we'll go ahead and an hour just to recognize when we say let tappy equal the UI tap gesture
to at this time make sure tap at the target which is self and the selector which we can add right now.
So I'm going to say at objective c and this is going to be called the phunk comment options tapped.
And adding our selector when do hash tag and say selector and then pass into the comments comments options
tapped.
There you go.
And then we need to add the gesture recognizes and with say Options menu dot and gesture recognizer
and the recognizer is tap.
All right next up we are going to need to create a another protocol.
So here I'm going to say protocol and the name is going to be Calment delegate.
And we're going to have our requirements so we're going to have one function that is going to be called
comment options tapped.
And we're going to pass in as an argument the comment and that is of type comment.
Very nice.
So right here we're going to need a couple of variables.
First off we're going to need to be able to keep track of the comment at the State of the entire comment
cells going to save our or private move are comment type comment and here and configure So we're going
to set that we're going to say self that comment is equal to comment and then we're going to need a
delegate.
So when I say private var delegate type comment delegate don't make that optional.
And then here in our configure so all arguments passed into it we were going to say delegate delegate
type comment and delegate and optional.
And then here will say self-taught delegate is equal to the delegate that is passed into it.
And if you remember from our thought cell that is going to be our comments AVC.
All right so let's go ahead and work on how our comments of D.C. now to get that information from our
comments well we need to implement our comment delegate and we'll go ahead and use the automatic fix
here to implement that method.
So we're going to select and say fix and there we go.
So here is where we are going to be adding a layer.
OK.
So we need to.
Only other thing we need to do in here we need to adjust our configure cell we need to be able to pass
in the delegate which is self.
Meaning this comments of the sea.
And now let's see here.
Jumping back into our comment cell in our comments option tapped we are going to then call that delegate
and say common options tapped and we will pass in to the comment.
And just to make sure that this is working so far let's go ahead and print out in the comments Visi
up at the top in our comment options tapped the one required method for the comment delegate protocol.
We are going to say print comment dot username and we will run this and test it out and we come over
here and right here I have some comments already if you don't have any comments go ahead and just create
your comments and then go ahead and test this so if I click on it we should see printed out down here
J.B. which we do.
All right.
Perfect.
So that is working.
Very nice.
All right cool.
So now we have it working to wear on both our comments and our thoughts.
We have a gesture recognizer on this image that only shows up if the logged in user is the owner of
that thought or comment.
And then when we click on it we have a protocol and a delegate that works so that we can actually work
here inside the controller to take action when that is clicked and we have the comment or thought respectively
available to us to work with.
All right.
So in the next lesson we're going to work on actually creating these alerts and doing some fire story
deleting.
All right.
So we're going to learn how to handle that.
So hope you're excited for that.
And we'll see you in the next one.