目标C:在不同的类中使用NSMutableArray [英] objective C: use NSMutableArray in different classes
问题描述
在创建了NSMutableArray * arrayImage之后,我创建了两个UIImageView * image1和* image2,现在我要填充此数组
I created two UIImageView *image1 and *image2, after I created a NSMutableArray *arrayImage, now I want fill this array
arrayImage = [[NSMutableArray alloc] initWithObjects: image1, image2, nil];
但是我在ClassA中创建了UIImageView和NSMutableArray,但是我想在ClassB .m中的viewdidload中填充NSMutableArray,然后Xcode告诉我未声明image1和image2.我只是使用属性和合成.我该怎么办?
but I created UIImageView and NSMutableArray in a ClassA but I want fill the NSMutableArray in the viewdidload in .m of ClassB, then Xcode tell me that image1 and image2 are undeclared. I just used property and synthesize. What can I do?
推荐答案
您可以尝试这样的操作,必须在classA内保留对classB的引用,以便在要向classB中的数组添加视图时访问classB的属性通过classA的clasB属性.尝试这样的事情.
You can try something like this, you have to keep a reference to classB inside classA so that when you want to add a view to array in classB you can access classB's properties through classA's clasB property. Try something like this.
//ClassA .h file
#import @"ClassB.h"
@interface ClassA : UIViewController {
UIImageView *view1, view2*;
ClassB *classB;
}
@end
//Inside ClassA .m file
-(void)viewDidLoad{
//construct view1 and view2 here or make the IBOutlets and link them in IB
classB = [[ClassB alloc] init];
[classB.imageArray addObject:view1];
[classB.imageArray addObject:view2];
}
//ClassB .h file
@interface ClassB : UIViewController {
NSMutableArray *imageArray;
}
@property(nonatomic, retain) NSMutableArray *imageArray;
@end
//Inside ClassB .m file
@synthesize imageArray;
-(id)init{
if (self = [super init]){
imageArray = [[NSMutableArray alloc] init];
}
return self;
}
-(void)dealloc{
[imageArray release];
[super dealloc];
}
这篇关于目标C:在不同的类中使用NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!