Passing data between view controllers


#1

Page 119 discusses how to pass data between view controllers, and on page 220, it says [quote]…having all of the data in the root view controller and passing subsets of that data to the next UIViewController is a clean and efficient way of performing this task.[/quote] What happens if the data you want to pass is not in the root view controller? Say, for instance, I have a view controller that gets pushed after the root view controller and, in this view controller, the user taps a button that sets an int counter and pushes a third view controller, and I need that counter to do some stuff on the third view controller. Should I be setting this counter as a property in the root view controller, access it in the second view controller and set it after the button tap, and then access it again on the third view controller to use its value? Is there a way to get the value if I make it a property of the second view controller??


#2

well i guess it would depend on the structure of the application that you were building. but if the counter that you needed to access is something you will need outside of the scope of these two view controllers, i would recommend storing them in a model object that has been set up previously.

that is a great question, as moving data around in an app can be a confusing issue to deal with. but the more stickily you stick to the MVC (Model, View, Controller) paradigm, the happier you will be in the long run.


#3

It seems excessive to create a separate model class to hold a single (in this case) int value.

After some research, what I did was this:

In the first view controller (we’ll call it VC1), I have an instance variable for this int, and made the instance variable a property. In the method where I want to set this property based on user input, I said self.propertyName = theIntValue; where “theIntValue” was an actual int. We can avoid the semantics of what the actual int is. In the method I use to push the VC2 I say:

ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"TheNameOfMyXib" bundle:nil]; vc2.vc1 = self; // This gives me a pointer to the instance of VC1 that has the instance variable value I am interested in [[self navController] pushViewController:vc2];

Then in the second view controller (VC2), I have the following interface file: [code]@class VC1;
@interface ViewController2 : UIViewController {
ViewController1 *vc1;
}

@property (nonatomic, retain) ViewController1 *vc1;[/code]
This allowed me to call propertyName (via [vc1 propertyName]) and use the value set by the first view controller.

I’m still trying to wrap my head around why this works the way it does. I understand that it’s the particular instance of VC1 that holds the value I am interested in, and so in order to use that value outside of the instance, I need a pointer to that instance. My assumption is that I get that pointer by creating an instance variable called vc1, making it a property, and then assigning its value to the instance of ViewController1 that I am interested in prior to pushing vc2. I think I may have too many instance variables/properties (maybe I don’t need one instance variable or one property…in my head, the instance variables I would want to get/set come paired with a property declaration), so if that’s the case, let me know!