0

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?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

2 Answers2

0

use delegation pattern and pass your class a object to class b like

if your class b

id a;

@property (nonatomic, assign) id a;

and synthesize it.

now init your class b in class a then ,

b.a=self;

thats it now your can use in viewdidload in class b as

self.a.arrayImage 

and study delegate pattern in internet , you will have clear view. good luck

Kshitiz Ghimire
  • 1,716
  • 3
  • 18
  • 37
0

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];
}
Sabobin
  • 4,256
  • 4
  • 27
  • 33
  • mi classA is a @interface TableViewCell : UITableViewCell and when I write ClassB *classB; don't accept it. Why? (I wrote import) – cyclingIsBetter Apr 13 '11 at 10:27
  • Did you do #import @"ClassB" in your ClassA.h ? – Sabobin Apr 13 '11 at 10:47
  • What kind of error are you getting, compile time or runtime? Post your error please. – Sabobin Apr 13 '11 at 10:54
  • I understand...in ClassB I just have import of classA, how can I solve? – cyclingIsBetter Apr 13 '11 at 11:06
  • Have you implimented ClassB in a sperate file, and made sure it is called ClassB ? I cant really help properly without seeing your code, mabe edit your question and add ur code to it. – Sabobin Apr 13 '11 at 11:13
  • I just realized that in pode I posted the import was wrong it said #import @"ClassB" and it should be #import @"ClassB.h". Sorry about that! – Sabobin Apr 14 '11 at 09:24