0

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.

tentmaking
  • 2,076
  • 4
  • 30
  • 53
  • Where is `frame` coming from? Anyway, I'd start by throwing in a few NSLog statements to track the flow of execution. First, make sure all of these methods are actually being called, and second, make sure you're not having nil problems, or your frame isn't right or whatever. – nhgrif Jan 18 '14 at 19:51
  • Frame is there, it looks like I removed the line in the question on accident. I don't see any nil issues and I have been stepping through it but cannot figure out why it isn't working. – tentmaking Jan 18 '14 at 20:14
  • Can you edit the removed line back in? – nhgrif Jan 18 '14 at 20:16
  • sure, but I doubt that will help anyone solve my overall issue. ;) – tentmaking Jan 18 '14 at 20:17
  • 1
    The code you post here should be identical to the code you actually have, for numerous reasons. – nhgrif Jan 18 '14 at 20:20
  • Yes, I have been working with setNeedsDisplay. Using that or what I am doing now produces the same results. – tentmaking Jan 18 '14 at 20:25

1 Answers1

0

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
}
Eugene Gordin
  • 4,047
  • 3
  • 47
  • 80
  • Yes, that will work to change the color of a view. However, for my specific problem, say I have 5 of the same views. When I tap on each of the views, it should change the line color on that view and set the line color of each other view to a default color. What I am doing is very similar to how Radio Buttons work. Select one, it deselects the others. I have the base functionality down, but the redrawing is not working correctly. – tentmaking Jan 18 '14 at 20:52
  • add a property to your view class BOOL isTouched. When touch occurs change it to YES for that particular view. Then go trough views and call [self setNeedsDispay: YES];...inside of a drawRect check for isTouched...if it is YES one color, NO - back to default – Eugene Gordin Jan 18 '14 at 20:56