I have a for loop that is working fine. I add inside a new UIVIew subclass allocation, to be done in each loop iteration:
for (n=something; n>0; n--)
{
//(...)
MyView* theview=[[myView alloc]initWithFrame:self.frame];
float y=sin(DEGREES_TO_RADIANS(currentAngle));
float x=cos(DEGREES_TO_RADIANS(currentAngle));
theview.point1=CGPointMake(x, y);
// printf x & y here
[self addSubview:theview];
}
in fact, theview is allocated, but the point defined inside the loop is not the same as the point1 when I look in theview.
In fact, all iterations of theview seem to be allocated AFTER the end of the loop, as stated by a printf inside theview drawrect method that is called after all printf in all iterations of the loop are called.
If not clear, I see something like that:
x=1
Y=1
x=2
y=2
x=3
y=3
printf from the view
printf from the view
printf from the view
. Why?
Thanks in advance!
The views are allocated and initialized immediately. They’re also immediately added to the view hierarchy. They won’t be asked to render themselves, however, until the next time the view hierarchy is rendered. As this refresh is done on the main thread, which you block until you’ve finished allocating the views, your views’ -drawRect: method will never be called before you’ve at least returned from this method (possibly other methods as well depending on the current event chain and your current stack).