Return to Video

02-12 Presenting the Dice View Controller

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

02-12 Presenting the Dice View Controller

more » « less
Video Language:
English
Team:
Udacity
Project:
UD788 - UIKit Fundamentals
Duration:
03:20
There has been no activity on this language so far.

English subtitles

Revisions Compare revisions