为什么我的UIActivityIndi​​cator不停止旋转?

时间:2013-09-27 16:14:22

标签: objective-c uiactivityindicatorview

我在调用GCD进程之前有以下代码显示ActivityIndi​​cator。后台进程在完成或遇到错误时会抛出通知。我在错误处理程序中调用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);   
}

2 个答案:

答案 0 :(得分:3)

与每次UIKit调用一样,您需要在主线程上执行此操作。

只是做:

dispatch_async(dispatch_get_main_queue(), ^{
    [mIndicator stopAnimating];
});

它应该有效

答案 1 :(得分:0)

也许你太快重试了?

scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS

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