我在调用GCD进程之前,在下面的代码中显示了一个ActivityIndicator。后台进程完成或遇到错误时,将引发通知。我在错误处理程序中调用stopAnimating方法,但微调器不断旋转。为什么?
UIActivityIndicatorView *mIndicator;
@interface VC_Main ()
@end
- (void)viewDidLoad
{
[super viewDidLoad];
[NSLog(@"viewDidLoad");
// create indicator for download activity
mIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[mIndicator setCenter:CGPointMake([self getScreen].x /2.0, [self getScreen].y / 2.0)]; // landscape mode
[self.view addSubview:mIndicator];
// fire off a single interval
[NSTimer scheduledTimerWithTimeInterval:0.0
target:self
selector:@selector(timerTask:)
userInfo:nil
repeats:NO];
...
}
- (void) timerTask:(NSTimer *) timer
{
NSLog(@"DEBUG: timertask timeout");
[mIndicator startAnimating];
...
}
// if there is an error parsing xml downloaded from server, it notifies here
- (void) xmlError:(NSNotification *)note
{
NSLog(@"error parsing xml");
[mIndicator stopAnimating]; // this doesn't work
// fire off a refresh using retry timeout
[NSTimer scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS
target:self
selector:@selector(timerTask:)
userInfo:nil
repeats:NO];
NSLog(@"will retry in %d", TIMEOUT_RETRY_MINS);
}
与每个UIKit调用一样,您需要在主线程上执行此操作。
做就是了:
dispatch_async(dispatch_get_main_queue(), ^{
[mIndicator stopAnimating];
});
它应该工作
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句