Reputation: 59
I have the code file for my UIView set the the custom class of the view in the storyboard. But, when I run the app and the view is loaded, nothing happens? I don't get any of my NSLogs, nothing is drawn to the screen? Does anyone know what I'm doing wrong? Am I not inilizing something properly?
My code for the UIView:
- (id)initWithFrame:(CGRect)frame
{
NSLog(@"Drawling area loaded");
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void)drawRect:(CGRect)rect
{
CGPoint dotOne = CGPointMake(1, 1);
[squareLocations addObject:[NSValue valueWithCGPoint:dotOne]];
CGPoint dotTwo = CGPointMake(10, 10);
[squareLocations addObject:[NSValue valueWithCGPoint:dotTwo]];
CGPoint dotThree = CGPointMake(100, 100);
[squareLocations addObject:[NSValue valueWithCGPoint:dotThree]];
int numby = [squareLocations count];
for (int i = 0; i < numby; i++)
{
NSValue *pointLocation = [squareLocations objectAtIndex:i];
CGPoint tmpPoint = [pointLocation CGPointValue];
CGRect tmpRect = CGRectMake(tmpPoint.x, tmpPoint.y, 10, 10);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 5.0);
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
CGFloat components[] = {0.0, 0.0, 1.0, 1.0};
CGColorRef color = CGColorCreate(colorspace, components);
CGContextSetStrokeColorWithColor(context, color);
CGContextMoveToPoint(context, tmpPoint.x, tmpPoint.y);
CGContextAddLineToPoint(context, 300, 400);
CGContextStrokePath(context);
}
}
@end
Upvotes: 0
Views: 83
Reputation: 135
try add this code:
- (void)setup{
self.contentMode=UIViewContentModeRedraw;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (void)awakeFromNib{
[self setup];
}
Upvotes: 0
Reputation: 3389
I think that you should use the debugger to make sure that your squareLocations
ivar is not nil in the drawRect:
method. You can send any message to nil without any error and actually doing nothing in objective-c. Also the initialization of squareLocations
ivar in drawRect:
method can fix the problem.
Upvotes: 1