Passing data around in UINavigationBars


A new question:

Why doesn’t this work?

// First class

#import SecondClass.h

@interface FirstClass : UIViewController 
    IBOutlet UIButton *doSomething;
    IBOutlet SecondClass *secondClass;
    NSString *firstClassString;
-(IBAction) doSomethingPressed;
@property (nonatomic, retain) NSString *FirstClassString
@implementation FirstClass
@synthesize FirstClassString
    [secondClass setSecondClassString:firstClassString];

// Second Class
@interface SecondClass : UIViewController{
    NSString *secondClassString;
@property (nonatomic, retain) NSString *secondClassString;
@implementation SecondClass;
@synthesize secondClassString;

// another IBAction which leads to:
[self setSecondClassString:@"this should change the firstClassString, no?"];

So, basically, this is identical to the example provided in page 180 and 181 except that it passes an array instead of a string. Why doesn’t it work? In other words, I try to give the SecondClass a pointer to the firstClassString object.



Not quite sure what you are asking, but it seems like you have two view controllers. The first gives a string to the second. Then, you want the second to change the first’s string. On that assumption, the code you have will not work.

The first class has a ivar, firstClassString. It points at an object, and therefore, its actual value is the address in memory of that object. When you pass this object to the second view controller, it sets its ivar, secondClassString, to the same value as firstClassString. That is, this string object is pointed to by both firstClassString and secondClassString - they store values that are equal.

When you change secondClassString via a setter, you are only re-assigning secondClassString to point at a new object. firstClassString still points at the string it originally pointed at. If you wanted to change the contents of the string that firstClassString pointed at, you must make that string mutable and change the characters that make up the string.

Notice how that in Homepwner, you are passing around a Possession instance, which is made up of other objects (its ivars). To edit that Possession instance, you change the objects which make up the Possession - but the Possession object remains the same (stays in the same spot in memory).


Ok. Thank you much.