I need to make one instance of an array that can be access by multiple view controllers. It will contain objects that will be displayed in a table view and created by a modular view and then displayed in the table view. I can't figure out how to access it once I make a @property for it in the main view controller or the AppDelegate class.
5 Answers
You should make a singleton and the recommended way to do that in objective-c is to create a class and add a method that looks something like:
+ (YourClass *)sharedYourClass
{
static dispatch_once_t onceToken;
static YourClass *sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
Put the array as a property in your class.
//YourClass.h
@interface YourClass : NSObject
@property(nonatomic, strong)NSArray *yourArray;
+(YourClass *)sharedYourClass;
@end
And in every class you want to use your singleton start by importing YourClass.h
and then use it like:
NSArray *arr = [YourClass sharedYourClass].yourArray;
[YourArray sharedYourClass].yourArray = [[NSArray alloc] init];
etc..

- 2,773
- 1
- 19
- 24
-
+1 for singletons. They seem like more work to set up, but are so easy to work with. – bachonk Oct 24 '13 at 21:18
What I do is put the data I want shared, in your instance the array, in the AppDelegate. Then I define a protocol that the app delegate conforms to. This lets me access the data anywhere. For example, say I have an array I want everywhere:
First define a protocol:
@protocol ApplicationState<NSObject>
@property(nonatomic, strong) NSArray* data;
@end
Then make your app delegate conform to it:
@interface AppDelegate : UIResponder <UIApplicationDelegate, ApplicationState>
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic, strong) NSArray* data;
@end
Then reading and writing this shared object is simple:
id<ApplicationState> appState = (id<ApplicationState>) [UIApplication sharedApplication].delegate;
appState.data = [[NSArray alloc] init];

- 2,563
- 2
- 15
- 18
I am going to assume you can make the class of the array inherit from NSObject, and then pass it to the View Controller from there...

- 9
- 6
-
I tried this, but when I import the .h of the object nothing shows up when i type the name of the array to send a message to it. Could you please show me how to properly define it and make one instance of it to use for both view controllers and how to send a message to add an object to it? Thanks! – ahyattdev Oct 24 '13 at 19:14
You have 2 ways to do this:
1.- Instantiate 1 arra on the main class and pass the reference to each viewcontroller what you need.
2.- Make a singleton class to hold the array an use this in your project.

- 905
- 7
- 10
First create a class like this
//GlobalDataClass.h
@interface GlobalDataClass : NSObject
@property(nonatomic,retain)NSArray *myArray;
+(GlobalDataClass*)getInstance;
@end
#import "GlobalDataClass.h"
//GlobalDataClass.m
@implementation GlobalDataClass
@synthesize myArray;
static GlobalDataClass *instance =nil;
+(GlobalDataClass *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance = [GlobalDataClass new];
}
}
return instance;
}
@end
Then you can use it in your viewControllers like this:
-(void)viewDidLoad{
[super viewDidLoad];
self.dataObj = [GlobalDataClass getInstance];
NSLog(@"%@",self.dataObj.myArray);
}
Hope it helps!

- 3,612
- 6
- 25
- 35
-
Glad i could help @ahyatt645. But upon further research, it looks like Peter's solution using dispatch_once is apple's preferred method. It's twice as fast. – Alaa Awad Oct 25 '13 at 15:14