Question

I'm implementing what seems to be standard code for iOS activity indicator, but it's not showing. This is what I do, following advice from around here:

In viewDidLoad:

indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicator.frame = CGRectMake(0, 0, 80.0, 80.0);
indicator.hidden = NO;
indicator.center = self.view.center;
[self.view insertSubview:indicator atIndex:100]; // to be on the safe side
[indicator bringSubviewToFront:self.view];
[UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;

Then in viewWillAppear, just before calling my long process method:

[NSThread detachNewThreadSelector:@selector(threadStartAnimating:) toTarget:self withObject:nil];

My threadStartsAnimating:

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

Then I have a Parse.com method that does some work in the background:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {       
    if(!error) {
        // do some work
        [indicator stopAnimating];
    }
}

For all I can see, this should work. But it doesn't. What am I missing?

Was it helpful?

Solution

Whenever you want to manipulate your view (i.e. addSubview, etc) it is best to do so in viewWillAppear. viewDidLoad is too early to manipulate views since other methods will still alter it (i.e. setFrame).

So moving your current code from viewDidLoad to viewWillAppear should do the trick!

OTHER TIPS

The startAnimating should be called on the main thread and not in a new thread. So just call [indicator startAnimating]; in the viewWillAppear.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top