You can create a data controller singleton class and use it much like Apple does in their own frameworks. Here is an example of the singleton data controller class.
// MyDataController.h
#import <Foundation/Foundation.h>
@interface MyDataController : NSObject
@property (nonatomic, retain) NSMutableArray *data;
+(MyDataController*)sharedController;
@end
// MyDataController.m
#import "MyDataController.h"
MyDataController *__dataController;
@implementation MyDataController
@synthesize data;
-(id)init {
self = [super init];
if (self) {
data = [NSMutableArray new];
}
return self;
}
+(MyDataController*)sharedController {
if (__dataController == nil) {
__dataController = [MyDataController new];
}
return __dataController;
}
@end
In this class the iVar data
is a instance variable of the MyDataController class and __dataController
is a class variable. This singleton is self initializing when you call its public static +(MyDataController*)sharedController
method. You can globally access the data array like this.
[[MyDataController sharedController] data];
To make a call to this singleton you will need to #import "MyDataController.h"
in every class you want to access it from or add it to your pch file to access it globally
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "MyDataController.h"
#endif
By using this scheme for Objective-C singletons, you can add additional convenience methods to your class. The singleton will retain a reference to one object of its own type meaning that any additional variables can be declared as instance variables instead of declaring them as class variables.