开发者

NSMutableArray & Multiple Views

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

TheMainViewController displays a table that gets the information from anNSMutableArray. TheSecondaryViewController is used to add objects into the array.

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

开发者_如何学C

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];
}


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.


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.


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!


You have to declare a NSMutableArray and set the property in TestAppDelegate.h
eg:

@interface TestAppDelegate : NSObject  {
    NSMutableArray *arrayList;
}
@property (nonatomic, retain) NSMutableArray *arrayList;
@end

In main class, you have to set @synthesize for arrayList;

if you want to add an object in arrayList from SecondViewController just write the following code


TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate.arrayList addObject:object];

you can access this array from any of your classes (that you are using in your application);


TestAppDelegate *appDelegate = (TestAppDelegate*)[[UIApplication sharedApplication] delegate];
1. NSMutableArray *array = [appDelegate.arrayList retain]; 

or

2. NSMutableDictionary *dictionary = [appDelegate.arrayList objectAtIndex:index];

I think it will help you...

Thanks.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜