Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xcode4 从另一个viewController写入全局数组_Xcode4_Nsarray_Global Variables - Fatal编程技术网

Xcode4 从另一个viewController写入全局数组

Xcode4 从另一个viewController写入全局数组,xcode4,nsarray,global-variables,Xcode4,Nsarray,Global Variables,我在一个viewController中有一个数组,我想从另一个viewController写入该数组,所以我将该数组定义为全局数组,但我没有成功写入它 ViewController1.h extern NSArray *xArray; @property (nonatomic, retain) NSArray *xArray; NSArray *xArray; .. @property (nonatomic, retain) NSArray *xArray; .. @synthesize *x

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

ViewController1.h

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

@synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];
#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:@"a",@"b",nil];
ViewController1.xArray = zArray;
NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;
NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;
ViewController2.m

@synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];
#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:@"a",@"b",nil];
ViewController1.xArray = zArray;
NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;
NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;
我得到以下错误:
在“ViewController1”类型的对象上找不到属性“xArray”

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

AppDelegate.h

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

@synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];
#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:@"a",@"b",nil];
ViewController1.xArray = zArray;
NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;
NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;
ViewController2.m

@synthesize xArray;
NSArray *xArray;
xArray = [[NSArray alloc] init];
#import "ViewController1.h"
NSArray *zArray = [NSArray arrayWithObjects:@"a",@"b",nil];
ViewController1.xArray = zArray;
NSArray *tmpArray1;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray1;
NSArray *tmpArray2;
AppDelegate *mainDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
mainDelegate.xArray = tmpArray2;