我在viewController中有一个数组,我想从另一个viewController写入该数组,所以我将该数组定义为全局数组,但我无法写入它!
视图控制器1.h
extern NSArray *xArray;
@property (nonatomic, retain) NSArray *xArray;
视图控制器1.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">
找到解决方案,在应用程序委托中定义数组并在任何类中使用它
AppDelegate.h
NSArray *xArray;
..
@property (nonatomic, retain) NSArray *xArray;
..
@synthesize *xArray;
视图控制器1.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;