Problem with UIActivityIndicatorView on iPhone
I want to show UIActivityIndicatorView on my iphone on view change. I wrote the following code:
- (void)viewDidLoad
{
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(320/2.0, 460/2.0)]; // I do this because I'm in landscape mode
[self.view addSubview:spinner];
}
and on mouse click I want to change view and in between I want to show this indicator. So I write
-(IBAction)buttonClick:(id)sender
{
[spinner startAnimating];
ViewController *lController = [[ViewController alloc] initWithNibName: @"View" bundle:nil];
self.viewController = lController;
[lController release];
//
[[self mainController] dismissModalViewControllerAnimated:YES];
[lViewController.view removeFromSuperview];
[self.view addSubview: lController.view];
[spinner stopAnimating];
}
It doesn't display an indicator, so plz tell me where am I going wrong?
UIActivityIndicator animates the main thread (changing animation frames per loop). If you start, execute the code, and stop, it will never have a chance to animate (since it never exits the current loop).
Try to run the code on a background thread. This will allow the main thread to process the animation frames.
a source to share
It buttonClick
looks like you are adding a lController.view
"top" counter (which was added earlier in viewDidLoad
). It's hard to tell from your snippet what's going on with modal firing, so let's assume it's not the culprit.
You can try either calling [self.view bringSubviewToFront:spinner]
after adding a new subview, or else [self.view insertSubview:lController.view belowSubview:spinner]
to put your view under the spinner. You can also set a property hidesWhenStopped
on the counter YES
to automatically hide when it stops.
Another thing to keep in mind is that loading and switching views doesn't really take that long, so the counter might not show up if something happens too quickly.
a source to share