Return to Video

Passing the Model using prepareForSegue - Intro to iOS App Development with Swift

  • 0:00 - 0:03
    Here is why the movie quote is still playing.
  • 0:03 - 0:06
    Thus far in RecordSoundsViewController,
  • 0:06 - 0:09
    we have a function called audioRecorderDidFinishRecording.
  • 0:09 - 0:13
    And inside this function, what we are doing is saving the recordedAudio.
  • 0:13 - 0:15
    By the way, we are tracking two things about the audio.
  • 0:15 - 0:18
    Its file path and its title.
  • 0:18 - 0:20
    Now, even though we are saving the recorded audio,
  • 0:20 - 0:24
    we're not quite passing it from the first scene to the second.
  • 0:24 - 0:26
    If you want to play the sounds in the second U controller,
  • 0:26 - 0:28
    we need to pass this data to the second scene.
  • 0:28 - 0:30
    Let's do that next.
  • 0:30 - 0:34
    Now to do this, I will go back to RecordSoundsViewController and
  • 0:34 - 0:38
    introduce you to another function that we inherit from UIViewController.
  • 0:39 - 0:40
    It's called prepareForSegue.
  • 0:41 - 0:44
    And as I start to type it, Xcode autocompletes it for me.
  • 0:45 - 0:50
    This function, prepareForSegue, it gets called just before a segue is about to
  • 0:50 - 0:54
    be performed and therefore is a great place to pass any data.
  • 0:54 - 0:57
    In here, I will add an if statement and
  • 0:57 - 1:00
    check to see if my segue identifier is stopRecording.
  • 1:00 - 1:03
    Now this type of a check becomes really important when we
  • 1:03 - 1:07
    have multiple segues connected to the same ViewController.
  • 1:07 - 1:10
    All right, to pass data to this second ViewController,
  • 1:10 - 1:13
    we somehow need a way to access it in code.
  • 1:13 - 1:15
    And I can do that with this line.
  • 1:15 - 1:19
    Here I created a constant called playSoundsVC, and then defined its
  • 1:19 - 1:24
    appropriate type, and then used segue's destinationViewController property
  • 1:24 - 1:27
    to get a handle on the second ViewController in our app.
  • 1:27 - 1:32
    I also use the as keyword in Swift to convert this destinationViewController to
  • 1:32 - 1:36
    the correct type we are expecting, which is PlaySoundsViewController.
  • 1:37 - 1:41
    Okay, so now that we have a way to access playSoundsViewController in code,
  • 1:41 - 1:43
    let's think about how we are going to pass data to it.
  • 1:45 - 1:48
    We can get our data from this sender parameter.
  • 1:48 - 1:51
    Remember that sender is the object that initiated the segue,
  • 1:51 - 1:53
    which in our case was recordedAudio.
  • 1:54 - 1:58
    So I can write a line of code like this to retrieve the data.
  • 1:59 - 2:03
    So now I just need to pass this data to playSoundsViewController.
  • 2:03 - 2:07
    But wait, this guy, playSoundsViewController,
  • 2:07 - 2:11
    it doesn't really have a way of receiving any data just yet, does it?
  • 2:11 - 2:14
    So to fix this, what I will do is go to playSoundsViewController from
  • 2:14 - 2:20
    the navigator, and create a new variable called receivedAudio.
  • 2:20 - 2:23
    Now this variable is of type, RecordedAudio.
  • 2:23 - 2:24
    This was the new class we created.
  • 2:25 - 2:29
    Now if you go back to prepareForSegue, I can pass the data in this manner.
  • 2:29 - 2:33
    I can say playSoundsViewController.receivedAudio.
  • 2:33 - 2:36
    This is the new variable we created in playSoundsViewController.
  • 2:36 - 2:37
    This is now equal to data.
  • 2:38 - 2:40
    Cool, let's run this project.
  • 2:41 - 2:42
    All right, let me record something.
  • 2:43 - 2:45
    Is the app working now?
  • 2:45 - 2:47
    Let me play the sound.
  • 2:47 - 2:51
    >> My momma always said life was like a box of chocolates.
  • 2:51 - 2:52
    >> Hm.
  • 2:52 - 2:54
    The app is still playing a movie quote.
  • 2:54 - 2:56
    Care to guess why that is happening?
  • 2:56 - 2:58
    Enter your response in this box.
Title:
Passing the Model using prepareForSegue - Intro to iOS App Development with Swift
Description:

05-12 Passing the Model using prepareForSegue

more » « less
Video Language:
English
Team:
Udacity
Project:
UD585 - Intro to iOS App Development with Swift
Duration:
02:59

English subtitles

Revisions Compare revisions