0

Possible Duplicate:
Passing Data between View Controllers

I have two view controllers and I want to get some information from the previous view in my app. e.g:

In my app I go from the first page to the second. Depending on what button the user pushes, I want to change the info on the second screen. What's the fastest and easiest way to do this? I tried importing the class and rebuilding but that recreates the string obj and doesn't keep the info that I want.

Community
  • 1
  • 1

3 Answers3

1

In your second ("child") view controller, keep a property for the string (see section 9).

When you instantiate the second view controller and before you push it onto the stack from the first view controller, set the string property's value, e.g., retain the first controller's string:

mySecondViewController.infoString = myFirstViewController.infoString;

Make sure your second view controller manages the memory for the string (usually with a release message in the controller's dealloc method, assuming your property is defined with a retain).

A second option is to keep properties in your application delegate, or another singleton that manages data for the application. But the first approach is a bit more lightweight.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • what would that look like in code? –  Nov 19 '11 at 00:03
  • Read the link about properties. That link contains code snippets that explain how to set up properties in your class. Then it is simply a matter of setting that property, usually right after you create the second view controller and before you push it onto the navigation stack. – Alex Reynolds Nov 19 '11 at 00:07
  • Cool, but I'm still a bit lost. I tried setting up a property but how do you access it without re creating it in the other view controller? Thus re-creating the String obj. –  Nov 19 '11 at 00:22
1

Well there are (at least) two possibilities:

  • Add a property to the next view controller, do something like
NewVC *vc = [[NewVC alloc] init]; // or initWithNibName...
[vc setMyInformation:information];
  • Create a custom init method:
NewVC *vc = [[NewVC alloc] initWithMyInformation:information andNibName:@"nibName" bundle:nil]; // well you should get the point...
cli_hlt
  • 7,072
  • 2
  • 26
  • 22
0

If I understand you correctly, what you need is to create an instance variable in vc2. And then when you create an instance of vc2 from vc1 you can access that iVar to assign value, etc. before you present vc2. Here is an example:

In ViewController2.h file:

@interface ViewController2
{
  NSString *string2;  //create an instance variable
}

@property (nonatomic, retain) NSString *string2;

In ViewController2.m file:

@implementation ViewController2
@synthesize string2;

In ViewController1.m file:

@implementation ViewController1

//ViewController2  *viewController2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];  //one way to instantiate 

viewController2.string2 = @"whatever string";  //here you assign the value to the instance variable string2 in viewController2



 //[self.navigationController pushViewController:childController animated:YES];  //etc. it depend on how you present viewcontroller2 
user523234
  • 14,323
  • 10
  • 62
  • 102
  • Though I wish I could use this, even though I imported it, It keeps saying that the property is not found. –  Nov 19 '11 at 02:52
  • Maybe put your project aside and work on a demo test app, so that you can kick the tires and learn properties. – Alex Reynolds Nov 19 '11 at 08:04