tags:

views:

1039

answers:

2

i want to pass a variable from a viewcontroller to another the way i do is at the first viewcontroller header file i declared a variable

1.h:

NSString *string;

and at the second viewcontroller i have import the 1.h in my 2.m file,the way i call the variable is

2.m:

NSString *string2 = 1.string

however it return an error can somebody teach me how to do,because of i dont have the strong basic at Object Oriented programming ,thanks

A: 

Although it is possible to directly access members variables like that (using the -> operator) it is not recommended.

The correct way is to provide an accessor to get/set your member variables.

In Objective-C 2.0 (iPhone and OSX 10.5) you can easily do this using the "property" keyword. As part of the property syntax you can also express how you wish "set" objects to be treated.

retained - the previous object will be released and the new one retained copy - the object will be copied assign - the object will be assigned.

These are the basics, I suggest you read up more about properties.

The below shows how you would use properties in your example. Note that because we are dealing with an NSString, which is an NSObject derived class, we use the "retain" option to ensure the reference counts are correctly updated.

// 1.h

@interface ViewController1 : UIViewController
{
// declare our variable
NSString* _string;
}

// declare 'string' as a property
@property (retain) NSString* string;

// 1.m
// implements the property for string
@synthesize string = _string;

// constructor for ViewController1
-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
  if (self = [super initWithNibName: name bundle: bundle]) {
    // Initialize the string here.
    self.string = @"Hello World";
  }
}

// 2.m
NSString* oldString = view.string;
view.string = @"New String";
Andrew Grant
hi andrew i have followed the code u provide however it return no error but the string still cannot pass from viewcontroller1 it return the string value is NULL,can tell me what is the reason
I have updated the example to show initializing the string in the constructor. It's always a good ideal to initialize your variables like this.
Andrew Grant
+1  A: 

Just defining and declaring the two strings is not enough. That makes sure that each class has a variable called string or string2 - but when your program is running, it is actual objects that must refer to specific instances of string1 (or string2).

It's like designing a house with a letterbox - the letterbox is there on the plan of the house, but nothing happens until a specific letter gets sent to a specific house.

What you need to do is wire up the actual instances of your class, possibily in an init method, like this:

// 1.h

@interface ViewController1 : UIViewController
{
// declare our variable
NSString* string1;
}

// declare 'string1' as a property
@property (retain) NSString* string1;

// 1.m
// implements the property for string1
@synthesize string1;

// 2.h
@interface ViewController2 : UIViewController
{
// declare our variable
NSString* string2;
}

// declare 'string2' as a property
@property (retain) NSString* string2;

// 2.m

- (id)initWithTitle:(NSString*)aTitle andString1:aString
    {
if (self = [super init])
 {
  self.title = aTitle;
  self.string1 = aString;
 }

return self;
}

Then in 1.m, you create the second controller, and wire the strings up, like this:

// 1.m
mySecondController = [[ViewController2 alloc] initWithTitle:@"Controller 2" andString:string1];
Jane Sales