What you want to do is add a Cocoa property in your main view controller that references the NSMutableArray
instance which you instantiate and populate with elements.
In the header:
@interface MainViewController : UIViewController {
NSMutableArray *myMutableArray;
}
@property (nonatomic, retain) NSMutableArray *myMutableArray;
@end
In the implementation, add the @synthesize
directive and remember to release
the array in -dealloc
:
@implementation MainViewController
@synthesize myMutableArray;
...
- (void) dealloc {
[myMutableArray release];
[super dealloc];
}
@end
You also want to add this property to view controllers that are subordinate to the main view controller, in the exact same way.
In your main view controller, when you are ready to push the subordinate view controller, you set the subordinate view controller's property accordingly:
- (void) pushSubordinateViewController {
SubordinateViewController *subVC = [[SubordinateViewController alloc] initWithNibName:@"SubordinateViewController" bundle:nil];
subVC.myMutableArray = self.myMutableArray; // this sets the sub view controller's mutable array property
[self.navigationController pushViewController:subVC animated:YES];
[subVC release];
}
Likewise in your subordinate view controller, it will need to set its subordinate's mutable array property accordingly, when it pushes its own view controller.
By setting references in this way, each view controller is pointed at the same mutable array, containing the desired elements.
To use the array, just call self.myMutableArray
, e.g. [self.myMutableArray addObject:object]
.