2

Button in drawtestViewController.m is linked to pushPoint action in ChalkBoard.m. It populates pointArray with CGpoint objects. This array is full of objects everywhere, except when viewed from drawRect. It seems as from drawRect I'm somehow referencing the empty copy of original array, but I can not find any error ...

I have uploaded the whole project at http://dl.dropbox.com/u/42099382/drawtest.zip


Button action in drawtestViewController.m

- (IBAction)myDo {
   NSLog(@"-Button was pressed");    
  [self.board pushPoint:CGPointMake(100, 100 )];
  [self.board pushPoint:CGPointMake(10, 100 )];
  [self.graphicView setNeedsDisplay];
}

//  ChalkBoard.h
#import <UIKit/UIKit.h>

@interface ChalkBoard : UIView
-(void)pushPoint:(CGPoint)point;
@property (nonatomic,retain) NSMutableArray *pointArray;
@end

//ChalkBoard.m
#import "ChalkBoard.h"

@implementation ChalkBoard
@synthesize pointArray;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    }
    return self;
}

-(NSMutableArray *)pointArray 
{
    if(pointArray == nil) pointArray = [[NSMutableArray alloc]initWithCapacity:10];
    return pointArray;
}

-(void)pushPoint:(CGPoint)point{
    // this is called from drawtestViewController.m by button press
    // in here the array is full and growing by each button press
    [self.pointArray addObject:[NSValue valueWithCGPoint:point]];
   NSLog(@"-pushPoint executed, size is %u, array: %@",[self.pointArray count], pointArray);
}

- (void)drawRect:(CGRect)rect{
// here array is always empty
NSLog(@"-DrawRect executed, size is %u, array: %@",[pointArray count], pointArray);
    if ([pointArray count] >1){   
       ... some irrelevant code
    }
}

@end

NSLog output

2012-04-09 11:34:39.145 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:41.261 drawtest[5260:f803] -Button was pressed
2012-04-09 11:34:41.262 drawtest[5260:f803] -pushPoint executed, size is 1, array: (
    "NSPoint: {100, 100}"
)
2012-04-09 11:34:41.264 drawtest[5260:f803] -pushPoint executed, size is 2, array: (
    "NSPoint: {100, 100}",
    "NSPoint: {10, 100}"
)
2012-04-09 11:34:41.266 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)
2012-04-09 11:34:42.825 drawtest[5260:f803] -Button was pressed
2012-04-09 11:34:42.826 drawtest[5260:f803] -pushPoint executed, size is 3, array: (
    "NSPoint: {100, 100}",
    "NSPoint: {10, 100}",
    "NSPoint: {100, 100}"
)
2012-04-09 11:34:42.827 drawtest[5260:f803] -pushPoint executed, size is 4, array: (
    "NSPoint: {100, 100}",
    "NSPoint: {10, 100}",
    "NSPoint: {100, 100}",
    "NSPoint: {10, 100}"
)
2012-04-09 11:34:42.829 drawtest[5260:f803] -DrawRect executed, size is 0, array: (null)
omz
  • 53,243
  • 5
  • 129
  • 141
Janis Jakaitis
  • 327
  • 1
  • 8
  • 1
    Could it be that you unintentionally have two different ChalkBoard objects? One that's in the window so it gets drawn, the other that's connected to your `board` outlet, which gets the points pushed to it. Try logging `self` with "%p" in both methods. – Ken Thomases Apr 10 '12 at 04:21
  • I can not find anything like that :( I have uploaded the whole project at http://dl.dropbox.com/u/42099382/drawtest.zip – Janis Jakaitis Apr 10 '12 at 16:17

2 Answers2

1

Replace pointArray with self.pointArray everywhere in drawRect method. When you use pointArray the variable is used as is (without the getter method invocation). When you use self.pointArray then the getter method is used.

I will also advice you to leave the getter and setter to the compiler to generate and initialize your pointArray in the init method.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
giorashc
  • 13,691
  • 3
  • 35
  • 71
  • added self, but no change - (void)drawRect:(CGRect)rect{ NSLog(@"-DrawRect executed, size is %u, array: %@",[self.pointArray count], self.pointArray); } – Janis Jakaitis Apr 09 '12 at 09:52
  • Just to make you happy, your answer saved me after 10h of looking to my view :) Have a nice weekend! – Mário Carvalho Jul 20 '13 at 16:11
1

The problem is actually not in your Chalkboard class but in your drawtestViewcontroller. Your myDo action adds points to self.board, but sends the setNeedsDisplay message to self.graphicView, which is also a Chalkboard but not the same one that you added the points to.

So the second log message you're seeing actually comes from an entirely different object.

omz
  • 53,243
  • 5
  • 129
  • 141