2013-09-27 29 views
2

我有下面的代碼顯示一個ActivityIndi​​cator調用GCD過程之前。後臺進程在完成或遇到錯誤時會引發通知。我在錯誤處理程序中調用stopAnimating方法,但微調控制器仍在旋轉。爲什麼?爲什麼我的UIActivityIndi​​cator不停止旋轉?

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); 
} 

回答

3

與每個UIKit調用一樣,您需要在主線程上執行此操作。

只要做到:

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

,它應該工作

+0

是的,就是這樣。謝謝。 – wufoo

+0

超級有用答案。經過幾個小時的谷歌搜索和嘗試不同的解決方案後,我無法弄清楚這一點。 – Neeku

0

也許你過早重試?

scheduledTimerWithTimeInterval:TIMEOUT_RETRY_MINS 

它應該是秒,而不是幾分鐘。

+0

啊!命名變量時出錯。接得好! – wufoo

相關問題