Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

xcode - objective C: use NSMutableArray in different classes

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];

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...