tags:

views:

1694

answers:

4

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 UIActivityViewer 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?

+3  A: 

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;

Ben Gottlieb
A: 

Ben,

Thanks for the answer. I tried your code sample, but the final app doesn't actually display the activity view at all. If I don't include the "stop animation" part of the process, I can see the icon spinning away after the new image is downloaded from the server.

I'm starting to guess here that I need to load the image in a separate thread, so the UI itself can continue to function in a way while the resource is downloaded.

+3  A: 

By the way, for those users/visitors that come to this thread later on, the class name is actually named UIActivityIndicatorView.

A: 

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.

Jablair