0

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

ViewController1.h

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

ViewController1.m

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

视图控制器2.m

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

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

4

1 回答 1

0

找到解决方案,在应用委托中定义数组并在任何类中使用它

AppDelegate.h

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

ViewController1.m

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

视图控制器2.m

NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;
于 2012-05-20T17:12:38.080 回答