- Learn about segues, and use
-prepareForSegue:sender:
to pass data forward to destination view controllers - Use navigation controllers to control a stack of view controllers
UINavigationController
provides a means to manage the navigation of hierarchical content. Often in iOS, we'll have related content that requires the pushing of one view on top of another. The content in the destination view is often derived from the content that was selected in the previous screen. In this Lab, we'll explore using navigation controllers to push content from one view to another.
In the project for this lab, add 3 classes:
TeamMember
, anNSObject
;TeamViewController
, aUIViewController
; andTeamDetailViewController
, anotherUIViewController
.
The three classes serve the following functions:
TeamMember
is a model class that represents each member of your team. It should have a name (name
), phone number (phoneNumber
), birth city (birthCity
), birth state (birthState
), favorite band (favoriteBand
), and photo (photo
). (Hint: useUIImage
for the photo property. You can use the class methodimageNamed:
to retrieve assets in your assets catalog. We've included some images with this project Notice the blue folderImages.xcassets
in your file navigator? That's where you can find the images.)TeamViewController
manages a view withUIButton
s for each of your team members.TeamDetailViewController
is pushed onto the navigation stack when a button is pressed inTeamViewController
. It displays the details for the particular team member that was tapped in theTeamViewController
.
-
In the storyboard, set the custom class of the view controllers to the new subclasses you just created.
-
Control
-drag from each button on theTeamViewController
to theTeamDetailViewController
and select a "Show" segue. This makes it so that tapping any button performs a segue to theTeamDetailViewController
.Run the app and see that the buttons do transition to the detail view controller. However, there are two problems. First, the detail is presented modally, and there's no way to get back to the previous screen. Second, the detail view controller has no idea which button was tapped! Let's remedy those issues.
-
In order for the segue to work as expected, you'll need to embed
TeamViewController
in a navigation controller in your storyboard. Do that! -
Use the
-prepareForSegue:sender:
method ofTeamViewController
to pass the tapped user forward to the detail view controller. Think about the best way to do this. The detail view controller should set all of its labels and image based on the team member that was tapped. Should you expose theIBOutlets
of the detail view controller publicly, or is there a cleaner way?
- The
-prepareForSegue:sender:
method in yourTeamViewController
is passed aUIStoryboardSegue
argument which can be used to access the destination view controller of the segue. Since we know the destination is aTeamDetailViewController
, we can cast it to the appropriate type and set itsteamMember
property to the member that corresponds with the tapped button. How can you figure out what button triggered the segue? (There are multiple options -- segue identifiers or thesender
argument. Play around with each!)
View NavigationControllers on Learn.co and start learning to code for free.