-
Here we are in our
RollViewController.swift file.
-
And the first thing I want you to
do is delete any code that's in
-
the rollTheDice method.
-
Here in this method we're going to
present our custom view controller.
-
So how are we going to do that?
-
We might as well start by trying
to present it the same way we did
-
the ui kit view controllers.
-
So in that case we just construct our
DiceViewController and then present it.
-
Oh, but
also we need to set those dice values.
-
So we'll do that in these two lines.
-
Okay, let's see if it runs.
-
Okay, so far so good.
-
Oh, hm, got a problem.
-
What does the console say.
-
Okay.
-
Fatal error.
-
Unexpectedly found nil while
unwrapping an optional value.
-
Okay, so the green highlighting is
pointing to the first die property.
-
And we know if the first
die property is nil,
-
that's going to cause a runtime error.
-
Because first sty is
an implicitly unwrapped optional.
-
So why is first die nil?
-
If we look back at our object diagram,
-
we're reminded that these outlets to
our views were created in storyboard.
-
We created this instance of
DiceViewController without Storyboard so
-
that means these outlets, these views,
and this action don't exist yet.
-
So that's a problem.
-
So in order to get rid of this error
we need to get a fully assemble
-
DiceViewController from Storyboard.
-
So let's close the console and
-
then go back into our
rollViewControler.swf file.
-
So here's where we were presenting
our DiceViewController before in
-
the rollTheDice method.
-
You go ahead and delete this
reference to the DiceViewController,
-
and we're going to replace it.
-
In this line, first we get
a reference to a storyboard object.
-
And then we asked for
-
the DiceViewController by name,
using its storyboard identifier.
-
Let's take a closer look
at this new method,
-
instantiateViewControllerWithIdentifier.
-
This identifier is just a string.
-
It's the name that you give
the view controller in storyboard.
-
So let's double check that we
actually set that name in storyboard.
-
So if I open up the storyboard file and
click on the DiceViewController.
-
I can look in the utilities pane and
click on the identity inspector.
-
In the identity inspector, right here,
you'll see the storyboard identifier.
-
And we can see that yes, indeed,
it is set to DiceViewController.
-
Just for kicks, I'm going to go ahead
and delete this storyboard id and
-
the class id and
-
see what would happen if you forgot to
put the name of the view controller.
-
Now let's try and run it.
-
Okay?
So it compiles fine.
-
RollTheDice, oh,
terminating with uncaught exception.
-
Okay, so
if you get one of these exception errors
-
what you want to do is scroll
up to the top of the console and
-
then you'll get more
information about the error.
-
And here we can see
storyboard doesn't contain
-
a view controller with identifier,
DiceViewController.
-
So that's pretty much what we expected,
but we know how to fix that.
-
We'll just go back in the storyboard and
reset the name.
-
There, that ought to do it.
-
Now, lets go back into our
scrollViewController.swf
-
file, because I want to point
out one more thing about
-
that
instantiateViewControllerWithIdentifier.
-
What's going on at the end
of this line here,
-
where it says DiceViewController
as DiceViewController.
-
If you Alt click on this method,
you can see that it returns any object.
-
That's why we need to cast that returned
object as a DiceViewController.