我會創建一個類(下面的MyDataController)來管理數據,並使用共享實例從我的應用程序中的任何位置訪問它。
接口(MyDataController.h)
@interface MyDataController : NSObject {
NSMutableArray *myData; // this would be the collection that you need to share
}
+ (MyDataController*)sharedDataController;
// ... add functions here to read/write your data
@end
實現(MyDataController.m)
static MyDataController* sharedDataController; // this will be unique and contain your data
@implementation MyDataController
+ (MyDataController*)sharedDataController
{
if (!sharedDataController)
sharedDataController = [[[MyDataController alloc] init] autorelease]; // no autorelease if ARC
return sharedDataController;
}
// ... implement your functions to read/write data
@end
最後,從任何地方訪問這個靜態對象:
MyDataController *dataController = [MyDataController sharedDataController]; // this will create or return the existing controller;
那麼,你必須得到它在某處......在每個子視圖上創建一個屬性?使用類似於UITableView的數據源模式? – onnoweb 2013-02-21 16:56:27