pJosh
pJosh

Reputation:

Problem with UIActivityIndicatorView on iPhone

I want to show UIActivityIndicatorView on my iphone when it changing the view. I have written 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 button's click event I want to change the view and in between that time I want to show that indicatorView 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 is not displaying the Indicator, So plz tell me where I am wrong?

Upvotes: 1

Views: 2957

Answers (2)

Graeme Wicksted
Graeme Wicksted

Reputation: 11

UIActivityIndicator animates on the main thread (animation frames change per run loop). If you start, execute code, and stop, it never has a chance to animate (since it never exits the current run loop).

Try running your code on a background thread. This will allow the main thread to process animation frames.

Upvotes: 1

Ramin
Ramin

Reputation: 13433

In buttonClick it looks like you're adding the lController.view "on top" of the spinner (which was added earlier in viewDidLoad). It's hard to tell from your snippet what's going on with the modal dismissal so let's assume that's not the culprit.

You could try either calling [self.view bringSubviewToFront:spinner] after adding the new subview or else [self.view insertSubview:lController.view belowSubview:spinner] to put your view underneath the spinner. You may also want to set the hidesWhenStopped property on the spinner to YES so it automatically hides when you stop it.

Another thing to keep in mind is that loading and switching views may not actually take that long, so the spinner may not appear if things happen too fast.

Upvotes: 0

Related Questions