문제

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