Why does this loop work only once?

advertisements

Just want the screen to blink blue/red over and over for some time based on an odd or even condition, but it only runs once, instead of 30,000 times. What am I missing?

-(IBAction) changeBackgroundColor:(id)sender
{
    for (int y = 0; y < 30000; y++)
    {

    if(y % 2)
        {
            self.view.backgroundColor = [UIColor blueColor];
            colorView.backgroundColor = [UIColor redColor];  

        } else {

            self.view.backgroundColor = [UIColor redColor];
            colorView.backgroundColor = [UIColor blueColor];
        }
    }
}


The loop runs 30000 times, but the screen is updated only once. You need to drop out to the runloop for changes to UIKit objects to take effect. Probably you want to set up an NSTimer and toggle the background colour within the callback.