4.3. Passing Data From One Screen to Another

Problem

You want to pass data from one scene to another using storyboards.

Solution

Use segue objects.

Discussion

A segue is an object, just like any other object in Objective-C. To carry out a transition from one scene to another, the storyboard runtime creates a segue object for that transition. A segue is an instance of class UIStoryboardSegue. To start a transition, the current view controller (which will get pushed out of the screen after the segue) receives the prepareForSegue:sender: message, where the prepareForSegue parameter will be an object of type UIStoryboardSegue. If you want to pass any data from the current view controller to the view controller that is about to appear on the screen, you need to do that in the prepareForSegue:sender: method.

Note

For this recipe to make sense, you need to have followed the instructions in Recipe 4.2 and created two view controllers inside a navigation controller on your storyboard.

Let’s implement the prepareForSegue:sender: method in the first view controller:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

  NSLog(@"Source Controller = %@", [segue sourceViewController]);
  NSLog(@"Destination Controller = %@", [segue destinationViewController]);
  NSLog(@"Segue Identifier = %@", [segue identifier]);

}

If you run this app now, you will see the results in the console window. However, you might note that the identifier is nil. Each segue has an identifier that uniquely identifies it. Since one scene can have more than one segue associated with it, it’s a good idea to give your segues identifiers that you can then detect in your view controllers and take action accordingly.

A segue object in Interface Builder is the connection between two scenes. Figure 4-23 shows the segue as an arrow between my first view controller on the left and the second view controller on the right.

Selecting a segue object in Interface Builder

Figure 4-23. Selecting a segue object in Interface Builder

Follow these steps to give your segue an identifier:

  1. Select your segue object in Interface Builder by clicking on it.

  2. From the View menu, select UtilitiesShow Attributes Inspector.

  3. In the Attributes Inspector, in the Identifier text field, simply write the identifier that you would like this segue to carry with itself.

When the storyboard runtime calls the prepareForSegue:sender: method in the current view controller to prepare it for the segue, the destination view controller has already been initialized in the segue object. Now this is your chance to pass any required data to the destination view controller. You can either set the data directly into a property of the destination view controller, or pass your data by calling a method on that view controller; it is really up to you. In this code, my second view controller is of class SecondViewController and I’ve given my segue the identifier of SimpleSegueToSecondViewController:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

  NSLog(@"Source Controller = %@", [segue sourceViewController]);
  NSLog(@"Destination Controller = %@", [segue destinationViewController]);
  NSLog(@"Segue Identifier = %@", [segue identifier]);

  if ([[segue identifier]
       isEqualToString:@"SimpleSegueToSecondViewController"]){

    SecondViewController *viewController = [segue destinationViewController];
    viewController.dataModel = ...; /* Write the code here */

  }

}

In this example code, the dataModal property is a hypothetical property declared and implemented in the view controller that is the target of our segue. This view controller is an instance of the SecondViewController that we have created for this project. The purpose of this example is to show how you can prepare your view controllers for a segue and populate necessary data into the target view controller.

See Also

Recipe 4.2

Get iOS 5 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.