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