I have a UIView
, in the view's drawRect
method I am using CGRect
to draw lines and UILabel
to draw a label onto the view.
I would like to change the color of the lines once the view is touched. When the view is touched, it notifies my ViewController
that there was a touch:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.controller viewTouched:self];
}
My ViewController
responds by removing the view from the superview, creating a new UIView
, and setting any necessary properties, including a new color for the lines, and re-adding the view to the superview:
-(void)redrawView:(SomeView *)view asSelected:(BOOL)selected
{
[view removeFromSuperview];
SomeView *newView = [[SomeView alloc] initWithFrame:view.frame];
newView.tintColor = (selected) ? [UIColor blueColor] : [UIColor redColor];
newView.controller = self;
[self.scrollView addSubview:newView];
}
The problem I am having is the UILabel never gets removed from the UIView. It keeps drawing a new UILabel on top of the old one(s).
Am I redrawing the view correctly? Am I redrawing the view in the most efficient manner? What is it that I do not understand about redrawing views?
Your help is much appreciated.
Try to place redrawView method code inside the drawRect in your UIView class.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.controller viewTouched:self]; // ask to refresh your view inside viewTouched
// by calling [self setNeedsDisplay:YES]; there
}