views:

580

answers:

4

When I call startAnimating on a UIActivityIndicatorView, it doesn't start. Why is this?

[This is a blog-style self-answered question. The solution below works for me, but, maybe there are others that are better?]

+1  A: 

If you write code like this:

- (void) doStuff
{
    [activityIndicator startAnimating];
    ...lots of computation...
    [activityIndicator stopAnimating];
}

You aren't giving the UI time to actually start and stop the activity indicator, because all of your computation is on the main thread. One solution is to call startAnimating in a separate thread:

- (void) threadStartAnimating:(id)data {
    [activityIndicator startAnimating];
}

- (void)doStuff
{ 
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];
    ...lots of computation...
    [activityIndicator stopAnimating];
}

Or, you could put your computation on a separate thread, and wait for it to finish before calling stopAnimation.

jonnyv
A: 

I used your suggestion. In my program the "doStuff" Method is the "searchBarSearchButtonClicked" Method.

So when I click the search button of the searchBar, then the AcitivityIndicator shows up and runs like hes supposed to do. As soon as I try to click another time on the search button, the "startAnimating" event again gets executed but the ActivityIndicator doesn't run anymore..

Do you have any suggestions?

Daniel
+1  A: 

I usually do:

[activityIndicator startAnimating];
[self performSelector:@selector(lotsOfComputation) withObject:nil afterDelay:0.01];

...

- (void)lotsOfComputation {
    ...
    [activityIndicator stopAnimating];
}
Frank Schmitt
A: 

Ok, sorry seems like I went through my code being blind.

I've ended the Indicator like this:

    [activityIndicator removeFromSuperview];
activityIndicator = nil;

So after one run, the activityIndicator has been removed completely.

Daniel