3

Im quite new to iphone development, I want to create a mutable array which can access and populate from different view controllers. How can I do it? If you can please give me a sample code

Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
smartsanja
  • 4,413
  • 9
  • 58
  • 106

3 Answers3

3

In MVC, you have views, controllers and models. You should push as much down as possible. Multiple views and controllers can operate on the same model that controls the data and business logic.

In your case, you have a simple set of shared data - a mutable array.

I would suggest create a class that contains that mutable array and exposes methods. A common pattern is for that class to be a singleton.

So, the multiple controllers would do.

MyModel *model = [MyModel sharedInstance];

Then both controllers can operate on it.

Here's a good article on the topic: http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html

singleton from apple: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32

Hope that helps.

bryanmac
  • 38,941
  • 11
  • 91
  • 99
  • thanks a lot for the help, Im able to solve it using my application delegate. I declare array in my delegate class and populate it from another view controller – smartsanja Aug 28 '11 at 13:18
  • 1
    Great - you can have one view controller be the delegate for the other view controller and it will work. As the app grows, that approach may get tough - as you need to share that data more and more, you'll be passing around other views that's holding your data. If you put it in another class that it's responsibility of holding the data, then it will be less coupled. Also, imagine you create a desktop Mac app - could share model. All of this is purity and best practice but stuff to keep in mind as designs get larger. – bryanmac Aug 28 '11 at 16:13
1

In your @interface YourClass decline array:

@property (nonatomic, retain) NSMutableArray *publicArray;

In your @implementation YourClass write this:

@synthesize publicArray;

If you will have reference yourClassVariable to that object you can access that variable using [yourClassVariable publicArray]; or yourClassVariable.publicArray;

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • thanks a lot for the help, Im able to solve it using my application delegate. I declare array in my delegate class and populate it from another view controller – smartsanja Aug 28 '11 at 13:18
1

In most cases its not needed nor helpfull to use global variables. If you have to use them the easiest way is to put the array in your AppDelegate.

You can access the Array (named array in AppDelegate here) with :

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];

NSArray *aArray= [appDelegate array];
Amandir
  • 679
  • 1
  • 7
  • 20
  • thanks a lot for the help, Im able to solve it like this myAppDelegate *appDelegate = (myAppDelegate *) [[UIApplication sharedApplication] delegate]; // populate appDelegate's array [appDelegate.arrSelectedanswers insertObject:strSelectedAnswer atIndex:0]; – smartsanja Aug 28 '11 at 13:16