views:

78

answers:

2

I can't get my activity indicator to work.

Here's what I've got-

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
}

The part of the code that is supposed to get it started and then ended--

...
    else if ([theSelection isEqualToString: @"Update statistics"])
    {
        [self startTheAnimation];
        [updateStatistics  updateThe2010Statistics];
        [self stopTheAnimation];
    }
...


-(void)startTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView startAnimating];
}

-(void)stopTheAnimation {
    [(UIActivityIndicatorView *)[self navigationItem].rightBarButtonItem.customView stopAnimating];
}
A: 

At least change:

   [activityIndicator hidesWhenStopped];

To:

   activityIndicator.hidesWhenStopped = YES;

Or remove that line, since YES is default.

progrmr
A: 

Most likely you're suffering from blocking the system's event thread: are you executing that method where you're calling [updateStatistics updateThe2010Statistics]; from some IBAction callback or any other method that is triggered by the system (like -viewDidLoad, -viewWillAppear or similar)?

In that case your long-running task will block the event thread which in turn cannot update your activity indicator. Try to do the following:

...
else if ([theSelection isEqualToString: @"Update statistics"])
{
  [self startTheAnimation];
  [self performSelectorInBackground:@selector(doUpdateStatistics) withObject:nil];
}
...

- (void) doUpdateStatistics {
  [updateStatistics  updateThe2010Statistics];
  [self performSelectorOnMainThread:@selector(stopTheAnimation) withObject:nil waitUntilDone:NO];
}

This will execute your statistics update on a second thread so that your event thread can properly update the activity indicator. At the end of updating the statistics, we're calling stop animation on the main thread (ie. the event thread) again to stop your activity indicator.

Benjamin