Using an Instance Variable inside drawRect

Discussion in 'iOS Programming' started by IssamTP, Jun 10, 2010.

  1. IssamTP macrumors newbie

    Jun 10, 2010
    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:

    // 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;
    // 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];
    PhotoListViewControls, inits PhotoList view with some data. Everything seems to works fine until I call drawRect method. I mean, if I write down:
    // Into PhotoList.m
    - (void)aMethodToPrintArrays{
    // 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.
  2. vocaro macrumors regular

    Mar 5, 2004
    I don't know, but are you absolutely certain that initWithData is being called?

    Also, your code has several memory leaks.
  3. IssamTP thread starter macrumors newbie

    Jun 10, 2010
    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.
  4. vocaro macrumors regular

    Mar 5, 2004
    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.
  5. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    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?

Share This Page