Question

I'm trying to display a loading icon while my iPhone app downloads a network resource, but I can't figure out how to make it show up correctly.

I searched around and found some details on the UIActivityView class, but the available example source code didn't work, and the documentation is kind of terse.

Could someone provide a simple example on how to use this class?

Was it helpful?

Solution

Assuming you've got a view controller set up, and would like to add a UIActivityIndicator to it, here's how you could do it:

(assume you've got a member variable called indicator, which you can use later to clean up)

For your interface (.h file):

UIActivityIndicator *indicator;

For your implementation (.m file):

Start the Animation

CGRect b = self.view.bounds;
indicator = [[UIActivityIndicator alloc] initWithActivityIndicatorStyle: 
                                             UIActivityIndicatorStyleWhite];
//center the indicator in the view
indicator.frame = CGRectMake((b.size.width - 20) / 2, (b.size.height - 20) / 2, 20, 20); 
[self.view addSubview: indicator];
[indicator release];
[indicator startAnimating];

Stop the Animation

[indicator removeFromSuperview];
indicator = nil;

OTHER TIPS

Ben answer looks pretty similar to what I'm doing - your guess about the thread is probably accurate. Are you using NSURLConnection to handle your downloading? If so, are you using the synchronous or asynchronous version? If it's the synchronous version and you're simply starting and stopping the animation around the synchronous call, then the UI isn't updating until after the you've stopped the animation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top