0

I use UIActivityIndicator in my app. I have written code for it as follows:

-(void)startSpinner {
    
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.hidden = NO;
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [self.view bringSubviewToFront:spinner];
    [spinner startAnimating];
}

I call this method on the UIButton's action event, and to remove indicator I write the code as follows:

-(void)stopSpinner {
    
    [spinner stopAnimating];
    [spinner removeFromSuperview];
    [spinner release];
}

on click on the button indicator appears but when I call -(void)stopSpinner method in view willAppear the indicator does not disapppear.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self stopSpinner];
   
}

Even I debug the code and I found that control also goes to the stopSpinner().

What is the problem here?

halfer
  • 19,824
  • 17
  • 99
  • 186
iPhone
  • 4,092
  • 3
  • 34
  • 58
  • Try making the UIIndicator in Interface Builder and see if you get the same situation. – SimplyKiwi Nov 25 '11 at 06:58
  • Check the address of spinner when you start it and when you stop it. I have a feeling that when you stop it, the `spinner` variable in your `stopSpinner` method is null. – Michael Dautermann Nov 25 '11 at 06:59
  • try adding some delay for stoping Spinner – Sunil Pandey Nov 25 '11 at 07:00
  • 3
    Why are you stopping a spinner in viewWillAppear? – NJones Nov 25 '11 at 07:02
  • Are all these methods in the same instance of the same class? Can you give use some idea of the order of operations here - I can't picture how you would be starting a spinner and then wanting to stop it in the same viewcontroller's viewwillappear method? – jrturton Nov 25 '11 at 09:05

1 Answers1

3

You might have potential leak in startSpinner because you always create UIActivityIndicatorView without releasing it. Change your method like this:

-(void)startSpinner {
    if (spinner){
        [spinner removeFromSuperview];
        [spinner release]; spinner = nil;
    }
    spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    spinner.frame = CGRectMake(137, 160, 50, 50);
    [spinner setHidesWhenStopped:YES];
    [self.view addSubview:spinner];
    [spinner startAnimating];
}

For stopping animation assign stopSpinner for another UIButton action. Cos viewWillAppear will fired early then you tap on any button.
ps. maybe you mean viewWillDisappear?

beryllium
  • 29,669
  • 15
  • 106
  • 125