Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

IssamTP

macrumors newbie
Original poster
Jun 10, 2010
5
0
Hello everybody, I'm new in the Mac programming world and I'm trying to learn to build some iPhone apps. I'm following the Stanford Course and I'm stuck at assignment 4, part 1. I have this code:

Code:
// PhotoListViewController.m
...
// This is PLVC initWithNibName method
	if ( [caller isEqual:@"Josh"] ) {
		NSMutableArray *images = [[NSMutableArray alloc] init];
		NSMutableArray *titles = [[NSMutableArray alloc] init];
		[images addObject:@"photo1.jpg"];
		[titles addObject:@"Urban Disaster"];
		[images addObject:@"photo2.jpg"];
		[titles addObject:@"Concrete Pit"];
		
		photoList = [[PhotoList alloc] initWithData:images andTitles:titles andAuthor:caller];
	}
	[photoList setNeedsDisplay];
	return self;
...
// PhotoList.h
@interface PhotoList : UIView {
	...
	NSMutableArray *imagesArray;
        ...
}
...
- (id)initWithData:(NSMutableArray *)images andTitles:(NSMutableArray *)titles andAuthor:(NSString *)author;
...
@end


// PhotoList.m
- (id)initWithData:(NSMutableArray *)images andTitles:(NSMutableArray *)titles andAuthor:(NSString *)author{
	if ( self = [super init] ) {
                // Arrays do have correct values.
		imagesArray = [[NSMutableArray alloc] initWithArray:images];
		titlesArray = [[NSMutableArray alloc] initWithArray:titles];
		authorIVar = [[NSString alloc] initWithString:author];
	}
	return self;
}
- (void)drawRect:(CGRect)rect{
	...
	NSLog(@"%@",imagesArray); // Arrays have null values
	for( NSString *images in imagesArray){
		CGRect frame = CGRectMake(x, y, 30, 50);
		labels = [[UILabel alloc] initWithFrame:frame];
	}
	return;
}
PhotoListViewControls, inits PhotoList view with some data. Everything seems to works fine until I call drawRect method. I mean, if I write down:
Code:
// Into PhotoList.m
- (void)aMethodToPrintArrays{
       NSLog(titlesArray);
}
// Into PhotoListViewController.m
       ... // bla bla yada yada
       [photoListViewController aMethodToPrintArrays];
I receive the right values, but when it comes the time to draw my labels and my images, seems that my IVars are all null :confused:.
Where am I doing wrong? :confused:
Thank you very much.
PS: Please :p I don't want the solution of the assignment!
PPS: My english is not the best, please feel free to ask me to repeat.
 
I don't know, but are you absolutely certain that initWithData is being called?

Also, your code has several memory leaks.

Yes. I logged data into the initWithData.
If you mean that I didn't put [var release] after alloc, I cutted them here to keep it short.
Thanks.
 
I didn't put [var release] after alloc, I cutted them here to keep it short.

Then perhaps there is something else you cut that is causing the problem, because I can't see anything wrong with the snippet you posted. Maybe if you post the entire project as-is I might spot something.
 
There are some odd things about your code and discussion. Your code doesn't directly call drawRect, does it? That's not how drawing works. Why do I see you creating labels inside your drawRect method? That is definitely a wrong approach. I don't see any drawing at all inside your drawRect method. Does it actually draw something? Do you addSubview your photolist view somewhere?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.