views:

36

answers:

2

I have a superview and a subview. Both are custom. During the drawRect of the superview, I am trying to add the subview, create an image from the drawn view, and remove the subview.

My code works fine when I run it and start with the subview already drawn in the superview. It seems like adding the subview (during the drawRect) doesnt draw it immediately, it has to wait for the next drawRect.

How do I add a subview and force it to draw immediately?

A: 

First, if you find yourself forcing a display, you're almost certainly doing something wrong.

Second, you shouldn't be adding subviews during drawRect, which is meant mostly to draw the view's current State.

Third, what are you actually trying to do? The broad, dumbed down description, please. It seems to me you're having a problem with the basic approach to your goal, so if you describe it beyond the very narrow focus of what isn't working for you, we might be able to suggest a better (and perhaps easier) solution.

Finally, in direct answer to your question, just tell the view to -display if you want it to display immediately ... but I don't think it's going to behave the way you're expecting it to.

Joshua Nozzi
"First, if you find yourself forcing a display, you're almost certainly doing something wrong."You are right. I should be setting it all up in viewWillDisplay and have all the drawing code in drawRect. I will try that.
joels
I was trying to make the parent view create the child view, once it was drawn perform a method on the child view, then remove the child view. I ended up just adding the child view, setting some props to make it perform the methods itself during the end of it's drawRect method, then removing itself. Thx Joshua!
joels
From the code you posted, it looks like you're trying to create an image of the parent view, then overlay some drawing onto that image. If that's the case, why not just create an image with -dataWithPDFInsideRect:, lock focus on that image, and draw directly to it? Why bother swapping views around, etc?
Joshua Nozzi
A: 

Use [view display] to immediately draw it.

Leibowitzn
that woudl just cause a loop if I did it in drawRect. thx though
joels