Objective c 为什么';我的UIActivityIndicator不能停止旋转吗?

Objective c 为什么';我的UIActivityIndicator不能停止旋转吗?,objective-c,uiactivityindicatorview,Objective C,Uiactivityindicatorview,在调用GCD进程之前,下面的代码显示了ActivityIndicator。后台进程在完成或遇到错误时抛出通知。我在错误处理程序中调用stopAnimating方法,但微调器仍在旋转。为什么? UIActivityIndicatorView *mIndicator; @interface VC_Main () @end - (void)viewDidLoad { [super viewDidLoad]; [NSLog(@"viewDidLoad"); // create i

在调用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);   
}

也许你重试得太早了

scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS

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

与每个UIKit调用一样,您需要在主线程上执行

只要做:

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

而且它应该会起作用

啊!命名变量时出错。抢手货是的,就是这样。谢谢。非常有用的回答。我在谷歌上搜索了几个小时,尝试了不同的解决方案,却没能弄明白这一点。