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?

有帮助吗?

解决方案

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!

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top