Passing Data from one View Controller to Another through Segues. Xcode fundamentals.

Every application uses data in one ay or another. When you are writing an iOS native application there are two ways to transfer data that I find most straightforward and easy to use.
One is passing data from one View Controller to another by utilizing the “prepareForSegue” method. This technique is good when you need to pass data in one direction – I will be explaining this method in this post.
In my next post I will explain how to use a specific data object that is accessible throughout your entire application. You can access this data object from every view and modify it from every view, and the changes in the object will be seen in every view. First though its better to gain understanding of how to pass data in one direction.

APPLICATION SCENARIO:
Let’s say you have an application, which allows the the user to create a list of the presents he or she want for their Birthday. When the user wants to add a present to the list they open a view which allows them to input the new present’s name.
This “Add New Present” View does not need to know nor receive information for what presents are already in the present list. It only outputs data when the user wants to input a new present wish.
In this case passing the new data from the “Add New Present” View to the list of all presents can be done easily in the following manner:

1. Inside your AddNewPresent_ViewController.m type in the following code:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//we'll be adding more here later.
}

2. Next Go to the view controller that displays all presents – we’ll name it AllPresentsList_ViewController for clarity. Inside the AllPresentsList_ViewController.h make your @interface look like the following code :

@interface AllPresentsList_ViewController : UITableViewController
{
NSObject * newPresent;
}

3. Then declare the newPresent property underneath like so:

@property (nonatomic, retain) NSObject * newPresent;

now your AllPresentsList_ViewController has a specific object which is dedicated to new birthday input.

4. Go back to the AddNewPresent_ViewController.m and make sure you tell this controller that you are going to be calling another view controller and their property from here. In our case we are going to be calling the AllPresentsList_ViewController’s newPresent property from our AddNewPresent_ViewController.m. To notify AddNewPresent_ViewController.m type in the following code before the @interface AddNewPresent_ViewController and after any #import declarations that might be already there.

#import "AllPresentsList_ViewController.h"

-this explains to Xcode that in the class AddNewPresent_ViewController.m we will be mentioning the AllPresentsList_ViewController.h class.

Next go back to the first method we mentioned: the “prepareForSegue”. Change the code to look like so:

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

AllPresentsList_ViewController * allPresentsController = ( AllPresentsList_ViewController * )segue.destinationViewController;
//next pass the data that the user has just inputted through the AddNewPresent_View.
allPresentsController. newPresent = myNewPresentInput;
}

– now once the segue has taken us back to the All Presents View, we will have our new present data already waiting for us there! Then we can add it to the rest of the presents inside our Presents list. Just imagine all the presents you could add !! 😉

NOTE: If you are using a Navigation Controller to control your views, you need to first define the UINavigationController which handles our AllPresentsList_ViewController before you can call on the AllPresentsList_ViewController’s newPresent property.

To do that change the code inside your “prepareForSegue” method like so:

UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
AllPresentsList_ViewController * allPresentsController = ( AllPresentsList_ViewController * ) navController.destinationViewController;
//next pass the data that the user has just inputted through the AddNewPresent_View.
allPresentsController. newPresent = myNewPresentInput;

-because the segue would be referring to the UINavigationController first instead of directly to the view poi have to first define the navigation controller which is responsible for the allPresentsView. Then you will pass data to the newPresent property from the navigation controller.

Now if you debug your application and add a new Present from the AddNewPresent_View and go back to the All Presents View – you will see that the newPresent property holds the name of yet another present you would like to have. Then you can just add your new present to the list of all presents! Yes ! You can wish for as many presents as you can think of !
Happy present wishing 😉

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.