7

I'm struggling with setNeedsDisplay. I thought it was supposed to trigger calls of drawRect: for the view for which it is called and the hierarchy below that if it's within the view's bounds, but I'm not finding that to be the case. Here is my setup:

From the application delegate, I create a view whose size is a square that covers essentially the whole screen real estate. This view is called TrollCalendarView. There is not much that happens with TrollCalendarView except for a rotation triggered by the compass.

There are 7 subviews of TrollCalendarView called PlatformView intended to contain 2D draw objects arranged around the center of TrollCalendarView in a 7-sided arrangement. So when the iPad is rotated, these 7 views rotate such that they are always oriented with the cardinal directions.

Each of the PlatformView subviews contains 3 subviews called Tower. Each tower contains 2D draw objects implemented in drawRect:.

So, in summary, I have TrollCalendarView with empty drawRect:, and subviews PlatformView and Platformview -> Tower that each have drawRect implementations. Additionally, Tower lies within the bounds of Platform, and Platform lies within the bounds of TrollCalendarView.

In TrollCalendarView I've added a swipe recognizer. When I swipe happens, a property is updated, and I call [self setNeedsDisplay] but nothing seems to happen. I added NSLog entries to drawRect: method in each of these views, and only the TrollCalendarView drawRect: method is called. Ironically, that is the one view whose drawRect method will be empty.

There is no xib file.

What do I need to do to ensure the drawRect method in the other subviews is called? Is there documentation somewhere that describes all the nuances that could affect this?

pasawaya
  • 11,515
  • 7
  • 53
  • 92
Victor Engel
  • 2,037
  • 2
  • 25
  • 46

2 Answers2

9

I'm struggling with setNeedsDisplay. I thought it was supposed to trigger calls of drawRect for the view for which it is called and the hierarchy below that if it's within the view's bounds

No, that is not the case. Where did you get that idea?

-setNeedsDisplay: applies only to the view to which it is sent. If you need to invalidate other views, you need to add some code to send -setNeedsDisplay: to them, too. That's all there is to it.

Kurt Revis
  • 27,695
  • 5
  • 68
  • 74
  • I got that idea from the answers to similar questions right here on stackoverflow. Just search subview setNeedsDisplay for samples. There are several. – Victor Engel Jul 14 '12 at 13:18
  • I will follow this suggestion unless I learn of another way. It at least matches my findings in testing. – Victor Engel Jul 14 '12 at 13:51
  • 1
    There's unfortunately a lot of noise on SO and a lot of blind-leading-the-blind. The questions you're looking at have very few votes, so probably not many people have seen them or had a chance to correct them. If you're just getting started, the Apple docs are a lot more reliable. – Kurt Revis Jul 14 '12 at 16:24
  • Thanks. It worked after I did the send to all relevant views. – Victor Engel Jul 20 '12 at 02:47
5

I think this is an optimization in the framework; if your subviews don't need to draw again, then this is a major performance improvement. Realize that almost anything animatable does not require drawrect (moving, scaling, etc).

If you know that all of your subviews should be redrawn (and not simply moved), then override setNeedsDisplay in your main view and do like this:

-(void) setNeedsDisplay {
    [self.subviews makeObjectsPerformSelector:@selector(setNeedsDisplay)];
    [super setNeedsDisplay];
}

I have tested this, and it causes all subviews to be redrawn as well. Please note that you will earn efficiency karma points if you somehow filter your subviews and make sure you only send that to subviews which actually need redrawn... and even more if you can figure out how not to need to redraw them. :-)

Chris Trahey
  • 18,202
  • 1
  • 42
  • 55
  • Thanks. I will try that. In my scenario, the 7 platform subviews will not need to be redrawn. Exactly two thirds of the remaining 21 subviews will be guaranteed to need to be redrawn. Knowing which of the 21 need to be redrawn is calculated efficiently in drawRect and can't be done more efficiently elsewhere. Just curious - how does the framework veto my decision (your optimization comment)? Seems like that defeats the purpose of having setNeedsDisplay in the first place. – Victor Engel Jul 14 '12 at 13:16
  • Nope. Doesn't work. This gets only the subviews. I need the subviews of those subviews to be redrawn. I've decided to do the work of walking the tree at the point where I need to set setNeedsDisplay. Thanks for the suggestion, though. – Victor Engel Jul 14 '12 at 13:52