Activity Indicator not showing up

iOSDev picture iOSDev · Dec 22, 2011 · Viewed 12.3k times · Source

I have two issues with activity indicator: 1. Activity Indicator not showing up on UIViewController

I have activity indicator added in .xib file. On button click it should start animating. and when response from server is received, before going to next page it should stop animating. I am doing it as follows:

activityIndicator.hidden = NO;

[activityIndicator performSelector:@selector(startAnimating) withObject:nil afterDelay:0.1];

[self.view bringSubviewToFront:activityIndicator];

....rest of code here....

activityIndicator.hidden = YES;

[activityIndicator stopAnimating];
  1. Activity Indicator not showing up on UITableView

For table view I am doing it same way but on didselectrowatindexpath...

For tableview I also tried adding activity view to cell accessory, but still not showing up

In both cases activity Indicator is not showing up.

Please help

Thanks

Answer

Firoze Lafeer picture Firoze Lafeer · Dec 22, 2011

If all this code is in one method or in response to one event, then none of the changes to the views are going be visible until you return to the event loop. You set the activityIndicator.hidden to NO and then set it again to YES before the UI has an opportunity to even refresh.

You also apparently stop the animation before you start it.

What you need to do is make the activity indicator visible here and start its animation. Then schedule the work to be done (start an asynchronous network connection, or put some work into a queue, or whatever it is you need to get done) and return from this method so that the UI can refresh, the indicator can be drawn, and the animation can actually start.

Then later at some point after the work is complete, you can hide the indicator and stop the animation. But you can't do all of that on the main thread within one single turn of the event loop. None of your changes will be visible because no drawing at all will happen here while this method is executing (assuming this is on the main thread)

I hope that makes sense?