It hey everybody this is Caleb with slopes dot com and in this video we're going to build our finished
goal v c which is where we're going to enter the amount of points needed to complete the goal.
And we're also going to set it up so that we can pass data from Create Goals.
VC over to finish goals.
VC so that at the end once we've given it an amount of points we can save it into a single goals object
to pass into our core data model so go ahead and pull open your X code project and load up main story
board and we're going to build ourselves another view controller.
So go ahead and search for you you controller and let's drag it on like so.
That looks pretty good.
And here's what we're going to do.
We are going to go ahead and copy this UI navigation style controller and pasted it over here.
We are going to go ahead and pin it from the left top and right zero.
And it already has a height constraint so we're good to go.
Next on this button we're going to right click and we're going to remove the Iby action that we set
in the previous controller.
And now this fully belongs to this view controller.
So let's go ahead and let's build this out first then we're going to create the code subclass and then
we're going to link up all of the Iby outlets and Iby actions.
OK.
So let's go ahead and let's put on a label.
We're going to drag it and put it right here and I'm going to make it so that it goes about about there.
That looks pretty good.
And let's go into the attributes inspector let's center the text here and let's change the font to custom
and the family to Avenir next.
OK that looks pretty great.
And let's make it a size 18.
Next we're going to go ahead and this or that it says how many points until complete.
And that looks good.
OK.
Let's pin that in place like so by pinning it.
Let's think let's do 20 20 and 20.
From the left top and right.
And let's give it a fixed height of 25.
Add those constraints and that will now stay in place.
Next we're going to go ahead and give it a UI textfield and yeah drag that on right there.
That looks awesome.
And for this one what we're going to do is we're going to set placeholder text of 0 K and we're going
to go ahead and center that and we're going to change this to be Avenir next.
Like so we're going to set it to be bold and we're going to make this size 36 actually you know what
let's make it even bigger 48.
No that's still too small maybe.
Dare I say 54.
Wow.
OK.
Awesome.
So that looks great.
But now we need to do is to change the font color to our dark green which is 6 DBC 6 3.
In case you're wondering so are placeholders Gray.
Our text is green but we need to fix the height of this so that it actually is the proper height which
it's not.
So to do that we need to put it into a stack view with our label.
So go ahead and click stack and I guess we didn't need to do those constraints until right now but that's
fine.
So go ahead and center that then click pin and we're going to pin 20 from the top 20 from the left and
20 from the right and we're going to give it a.
Well let's not give it a height constraint.
We don't need to but we do need to give it some space because it's a little bit cramped here.
So let's give it.
How about 10 points of space.
That looks pretty good.
All right.
Awesome.
So now we're actually going to duplicate this button by selecting it and then pressing commands see.
And we're going to click here on this view controller and press command V to paste it in.
That looks awesome but we need to right click and we need to remove this action that's connected to
create V-6.
We don't want that and we want to change this to say create goal.
All right.
Because once we enter in the goal description choose long term or short term and turn the amount of
points we have everything we need to create a goal supercool So now at this point we can go ahead and
set up some Iby actions but we need a subclass file before we can do that.
So right click on controller click new file Cocco touch class and we're going to call this finish goal
Visi and go ahead and create it.
Make sure the target is selected and add it into the controller group.
Supercool.
All right get rid of this.
We don't need.
Did receive memory warning.
And we also don't need this comment and extra space.
But now we need to go to main storyboard and set this controller to have the identity finish goals AVC.
So go ahead and in the class go ahead and type finish goal VC just like that and then open the assistant
editor so that we can link up the IB actions and Iby outlets.
All righty.
So select this view controller go to automatic and select finish goal Visi then what we need to do is
we need to go ahead and we're going to create an action for this button.
Create goal.
So we're going to say create goal button was pressed and I'm moving a little faster here because we've
already done this on quite a few VCs.
So let's do an outlet for this because we're going to need to bind it to the keyboard just like the
next button.
And the other controller.
So we'll just call this create goal button.
All right.
Next we need to go ahead and create an action for the I mean an outlet for this.
I'm going to call this maybe points textfield.
Super cool.
That's great.
And yeah that looks pretty good.
So I think what we're going to need to do is we're going to first need to bind the Create Go button
to the keyboard.
So go ahead and type create Go button bind to keyboard and then when this is pressed this is where we're
going to pass data into the core data gold model.
What we need to do now is to initialize data whenever we create this finished goal VC.
We need to initialize data and pass it into this view controller from the beginning so that we can pass
all of it when complete into this function when we create our goal.
So what we're going to do is we're going to go into finished goal AVC which we're already in and we
need to create a function called enet data in.
And in it data is basically going to take in a description from this text view and a goal type short
term or long term so we can just type in data description of type string and a type of goal type.
All right.
That looks great.
But now we need to do is we need to create some variables that are in this view controller that we're
not actually going to display their data on this screen anywhere but we're going to use these variables
to hold the values so that later we can pass them into the function that basically saves this model
into our core data model.
So we need a variable to hold the description and to hold the gold type.
So go ahead and type var goal description of type string and var goal type of type type type call type.
All right.
So cool.
We're going to go ahead and initialize these values by typing self doggle description.
That variable we just made up there and we're going to set its value to be in the description we pass
in when we create this view controller.
Next we're going to set self-growth type to be equal to the type we pass in and that's that once we
instantiate this view controller we will be able to call this and pass in the values and set them for
later.
So in order to actually pass these though we have to go back to create goal Visi like so and we're going
to set up how we actually instantiate this properly and initialize the data.
So when we press the next button that's when it's going to happen that's when we're going to initialize
the data that's when we're going to present the next to the seat.
So here's what going to happen.
We're going to go ahead and we need to set some conditions we need to first make sure that our goal
text view has even been entered into.
So go ahead and type if goal text view text is not equal to nothing and goal text view dot text is not
equal to what is your goal.
Because remember we're not using a placeholder.
We're actually using text in the text view.
And when we tap it we're going to clear it out.
We haven't done that yet but we're going to.
So we're going to just make sure that it's not what is your goal because that's not a goal and we're
going to make sure that there is something typed in there.
OK.
Now in order to present this view controller we're going to do it the same way that we did in Goles
VC.
We created an instance using the storyboard and we presented it.
OK.
So what we're going to do is the same thing.
Guard let finish goal.
The C equals storyboard whip's lowercase storyboard.
Instantiate controller with identifier and that identifier is finish goal visi.
But we have not yet actually set this that will cause a crash.
So go ahead and tap maned storyboard and select this view controller select the identity inspector and
storyboard ID.
We're going to type finish goal you see.
Now we can properly access it from create goal.
OK.
So we are now instantiating this.
But if we can't for some reason we're just going to return.
OK.
So supercool.
Now before we present this though this is where we initialize the data.
So go ahead and type finish go visi.
In its data and it looks like that function is not yet showing because we have not actually cast this
as the proper type finish goal.
VC.
So instantiate it and cast it as a finished goal VC and now we should be able to access it data.
So the description is going to be from gold text view text and since its a text view its text property
is optional so we need to force unwrap it.
But we know thats fine because weve already checked to make sure that it has text.
And of course the gold type is going to be whatever gold type we have set.
OK.
Remember we have a variable here keeping track of that and its always going to have a value because
we instantiate it from the get go with a value.
So we're good to go.
So we have now initialized the data.
Now all we need to do is type present detail and pass and finish goal.
VC when I go try it out I do.
Let's do it build and run.
I just press command our and your simulator should pop up and we should be able to check this out.
So let's press next or add.
I mean let's go ahead and create a go.
Eat healthy.
Eat healthy food can't type food twice a day.
All right.
And I'm going to make that a long term goal press next.
Ooh.
We have a problem we did not set constraints for our button.
So how many points until complete let's say maybe for five days.
Weird thing the font change there as well.
So it looks like we have some work to do.
Let's go back to our storyboard really fast and let's create our goal here or let's pin this button
to the bottom.
So go ahead and pin zero from the left bottom and right.
And it already has a height constraint carrying over from the previous view controller so just add those
three constraints and this textfield is where we're having some trouble here.
Now we are doing fine but we're going to go ahead and remove the border and we're going to go ahead
and just make sure that our front here is good.
You know we've set a font we've set it size and that looks good.
OK.
So let's let's just make sure that this stays the same size.
By giving it a height constraint I didn't want to but I'm actually going to let's just do 110 press
enter and now go ahead and build and run.
And let's see if everything is in its proper place.
OK press and what is your goal.
Oh you know what let's check.
We set a condition.
OK so if it says what is your goal or if it's empty.
It doesn't work.
Eat healthy eat healthy food twice a day.
Long term goal.
This list looks way better.
OK how many points still complete let's say for high and you see how it's changing the size there.
OK.
That's really weird.
So it appears that we do not have total control over this view controller but we need to go ahead and
figure out why that is.
We have not let's see oh you know what we have not yet inherited from you I textfield delegates so let's
do that really quick.
UI textfield delegate.
And let's go ahead and let's just set up in view did load.
Let's go ahead and make the delegate of our textfield this view controller.
So go ahead and type points textfield delegate equals self.
And when we build and run we should be able to set the delegate here.
Awesome.
And you know while we're at it we should also do this in create gold visi.
So let's go ahead and do that by typing UI text view delegate remembering that goal text view is a text
view not a text field.
And what we're going to do is we're actually going to clear out the text view when we start editing
it.
So to do that we can actually access one of the text views functions and it's called Text view did begin
editing.
So if we enter into that text you and it starts editing we're going to set goal text you don't text
to be equal to nothing.
We're going to clear it out just like a placeholder and we're going to set goal text for you dot text
color to be equal to a color literal of black.
So select the color literal there and select black.
Now we can build and run and whoops you know what we did not set the delegate I just built and run.
But we're going to set go text for you.
There we go.
Delegate to self.
Now I'm going to build and run.
I just pressed commander and now we should be able to go in and properly commend this.
So what is your goal.
And it clears that beautiful.
Oh but we good case our button works.
It's cleared and we can't send anything.
Eat Whoops.
Eat healthy food.
Twice a week.
Awesome.
Next how many points to complete.
We're going to say five.
And so it appears that the font is still changing size on us and we don't want that.
So let's go ahead and go into our story board here and let's figure out what the problem is the alignment
is center.
We actually want it to be fill.
So I don't think that's the problem but that could be part of it.
We have a minimum font size.
So let's turn off just to fit for the font size and for the keyboard type actually when we go into here
we can put in text and that does not make sense for our application.
So let's go ahead and set the keyboard type to actually just be number pad because we don't need it
to be text data.
OK.
That will actually cause problems with our application.
So build and run one more time let's go see if that fixed our problem with our weird font size issues
and we should be able to call this video good to go.
OK so let's go ahead and create a goal here.
Eat healthy
lunches for two days.
Short term goal and a let's see OK the button slides we have a number pad and two days oh Sarah button
our button goes away there.
That is not what we want.
So I think we're going to have to do actually to make this work is I think this is an issue with the
super view and the frame of the whole view controller so in order to fix this we're actually going to
go ahead and bring in a view like so and let's just drag it in like this.
And you know what I found this issue when you actually use the constraints of the safe area in beta
4 or the Superville whenever you enter text it's some for some reason updates the frame and the constraints
pull back down to where it's supposed to be.
So in order to circumvent this I actually have found that it works if you put this inside of a UI view
that is on top of the current view and then basically pin the constraints to this view instead of the
super view because then the outside constraints don't change.
So we're going to actually drop our create Go button into this view and we're going to drag that all
the way down to the bottom.
OK and we're going to also adjust this.
So the view that we just created.
Let's go ahead and pin this eight from the top zero from the right zero from the bottom and zero from
the left.
Add those constraints and now we can pin this button zero from the left bottom and right and add those
constraints.
Now it's white.
So it's going to blend in with the background and that's fine.
So let's go see if this fixed our problem and then we'll move on to the next video.
So we're building and running.
And here we go.
Loading up.
Eat healthy food.
Twice a week.
I love how it started with twice a day and now it's twice a week.
OK.
So how many points until complete.
We're going to say two points and notice our button doesn't go anywhere.
Perfect.
That's exactly what we want.
And now create gold is going to be the button that's going to save our data into our persistent store
and we're going to do that in the next video.
But we're going to basically save it then we'll dismiss this view controller and we're going to go ahead
and show it in the table view very very soon.
It's very exciting.
So amazing work building out this amazing view controller.
We're almost done guys we're on our way.
Next we're going to go ahead and write a function to save our data into our persistent store using core
data.
Then we're going to write a function to fetch all that data and fill an array full of goals and then
we're going to fill our table view with that we're so close I can't even believe it.
So awesome awesome job guys all see it in the next video.
Let's head over there now.