Totally confused here.
I have a PARENT UIViewController that needs to pass an NSMutableArray to a CHILD UIViewController. I'm expecting it to be passed by reference so that changes made in the CHILD will be reflected in the PARENT and vice-versa. But that is not the case. Both have a property declared as ..
@property (nonatomic, retain) NSMutableArray *photos
;
Example:
In PARENT:
self.photos = [[NSMutableArray alloc] init];
ChildViewController *c = [[ChildViewController alloc] init ...];
c.photos = self.photos;
...
...
...
In CHILD:
[self.photos addObject:obj1];
[self.photos addObject:obj2];
NSLog(@"Count:%d", [self.photos count]) // Equals 2 as expected
...
Back in PARENT:
NSLog(@"Count:%d", [self.photos count]) // Equals 0 ... NOT EXPECTED
I thought they'd both be accessing the same memory. Is this not the case? If it isn't ... how do I keep the two NSMutableArrays in sync?
UPDATE with some more code ...
IN PARENT UIViewController:
- (void)loadView {
self.photos = [[NSMutableArray alloc] init];
// Create view for root controller
UIView *rootView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = rootView;
self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"green_felt_bg.jpg"]];
[rootView release];
ChildViewController *c = [[ChildViewController alloc] initWithNibName:@"ChildView" bundle:nil];
self.childViewController = c;
[c release];
self.childViewController.photos = self.photos;
self.childViewController.delegate = self;
[self.view insertSubview:self.childViewController.view atIndex:0];
}
In the CHILD ViewController I'm just adding objects into it. Calling a delegate method when it is done adding objects ... which the PARENT handles. Nothing else significant going on. Code child is using to modify its "photos" property ..
[[self mutableArrayValueForKey:@"photos"]
addObject:[photo resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(200.0f, 300.0f) interpolationQuality:kCGInterpolationLow]];