从另一个viewController写入全局数组

时间:2012-05-19 23:04:23

标签: xcode4 nsarray global-variables

我在viewController中有一个数组,我想从另一个viewController写入该数组,所以我将该数组定义为全局但我没有设法写入它!

ViewController1.h

extern NSArray *xArray;
@property (nonatomic, retain) NSArray *xArray;

ViewController1.m

@synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];

ViewController2.m

#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:@"a",@"b",nil];
ViewController1.xArray = zArray;

我收到以下错误: 在'ViewController1'类型的对象上找不到属性'xArray'

1 个答案:

答案 0 :(得分:0)

找到解决方案,在app delegate中定义数组并在任何类

中使用它

<强> AppDelegate.h

NSArray *xArray;
..
@property (nonatomic, retain) NSArray *xArray;
..
@synthesize *xArray;

<强> ViewController1.m

NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;

<强> ViewController2.m

NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;