質問

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