views:

75

answers:

4

I am trying to write an application that has a NSMutableArray that needs to be accessed and modified in a different View.

The MainViewController displays a table that gets the information from an NSMutableArray. The SecondaryViewController is used to addObjects into the array.

How do I go about this without declaring it as a global variable?

EDIT:

This is what I have so far:

(MainView .m)

#import "arrayTestViewController.h"

@implementation arrayTestViewController

-(void)viewDidLoad{
    myArray = [[NSMutableArray alloc] init];

}
-(IBAction)showArray{
    NSLog(@"Array Count: %d",[myArray count]);
}   

-(IBAction)addToArray{

[myArray addObject:@"Test"];
[myArray addObject:@"Test2"];
NSLog(@"Array Count: %d", [myArray count]);

}

-(IBAction)switchViews{
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    [screen release];
}

(SecondViewController .m)

#import "SecondViewController.h"
#import "arrayTestViewController.h"

@implementation SecondViewController



-(IBAction)addToArray{
    // Trying to make a method add to myArray in the Main, but not sure of the syntax
    // I'm guessing localizing another array and pointing to the original?

}

-(IBAction)switchBack{
    [self dismissModalViewControllerAnimated:YES];
}
A: 

You should check out the Dependency Injection pattern - http://en.wikipedia.org/wiki/Dependency_injection

You can pass in a reference (pointer) to the NSMutableArray when you initialize the SecondaryViewController, and save it in a local instance variable. Then at anytime, when the SecondaryViewController needs to access the array, it can use its reference.

Michael Lamb
Sharing mutable state is not a good idea and is not at all idiomatic in Cocoa. Better to have the SecondaryViewController get a reference to the MainViewController and *ask* it to modify the array.
Chuck
A: 

Pass the array as a parameter in the SecondaryViewController's constructor thus allowing SecondaryViewController to add objects to it, objects that will be displayed as the MainViewController becomes again visible and you reload the table data.

luvieere
A: 

How do you display the SecondaryViewController? If you like, you could define a property in SecondaryViewController header like so:

{
NSMutableArray *theArray;
}

@property (nonatomic, retain) NSMutableArray *theArray;

Then you have to @synthesise it just below the @implementation

@synthesise theArray

In MainViewController just after you create the SecondaryArrayController you can do

theSecondaryViewController.theArray =theArrayWhichYouHaveCreated;

Now both objects have a pointer to the same object. Read in one, and write in the other! I have not tested this code, but it should work. If not, please comment!

Tom H
Sharing mutable state is not a good idea and is not at all idiomatic in Cocoa. Better to have the SecondaryViewController get a reference to the MainViewController and ask it to modify the array.
Chuck
This is the error I get in the header file when I tried what you said. error: statically allocated instance of Objective-C class 'NSMutableArray'
Antonio
How do I reference it, this is the syntax I am missing :( Thank you guys for trying to help :)
Antonio
@Antonio: That just means you're writing `NSArray something` rather than `NSArray *something`.
Chuck
@Chuck, or better, to have this kind of code not in the view controller stuff at all, but to create a class which manages all of this (as a singleton maybe?) to fit in with mvc. It depends on how it's going to be used...
Tom H
A: 

Have one controller be in charge of the array and have all access to the array go through that controller. For example, give MainViewController a reference to SecondaryViewController and set the SecondaryViewController as the table's data source.

Chuck
I have reference to the SecondaryViewController in the main header.
Antonio
This is what I wanted to do. Hold all methods manipulating the array in the main controller, then just call the methods from the secondaryViewController. I just don't know the syntax to do this from the secondaryViewController. Can anyone show me or point to somewhere I can learn it?
Antonio