0
votes

I've seen this question posted before but I cannot find the answer..

As expected, drawRect gets called automatically when the program launches. However when I call [self setNeedsDisplay] drawRect does not get called anymore and I cannot understand the reason why...

//DrawView.m

[self setNeedsDisplay];

-(void)drawRect:(CGRect)rect{

NSLog(@ "Called_1");

//DRAW A PATH
 }

Maybe using self is incorrect. I have also tried using DrawImage but still it doesn't work.

//DrawView.h

@interface DrawView : UIView {

UIImageView *drawImage;
}

//DrawView.m

-(id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = CGRectMake( 0, 0, self.frame.size.width, self.frame.size.height );
    [self addSubview:drawImage];
}

return self;
}
1
drawRect will not be sent unless the view is visible in the UI.David H
Good point. Thanks! I wasn't seeing it because it was happening on a different layer. I have multiple UIViews stacked on top of each other. Do you know how I can send the SetNeedsDisplay command to one specific UIView? Now I have no control as to where the shape is displayed. Cheersjeddi
You need to construct a path to it somehow. You can use the tag property, then you can ask the container [view.subviews viewWithTag:5];David H

1 Answers

0
votes

Usually when you want to have a view redraw you should call:

[self setNeedsDisplay: YES];

Granted I have never build on iOS, on OSX this code works every time. Also, for example, if you want your delegate to call a redraw for a view named someView:

[someView setNeedsDisplay: YES];

Note: that YES is a macro defined by obj-c that is just a value of 1.