为什么我的UIA活动指标不停止旋转?



我在调用GCD进程之前有下面的代码显示活动指示器。 后台进程在完成或遇到错误时引发通知。我在错误处理程序中调用 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];
});

它应该工作

也许你重试得太早了?

scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS

它应该是几秒钟,而不是几分钟。

最新更新