3

我想在默認NSNotificationCenter調用的方法中使用postNotificationName停止指標的動畫。 所以我在主線程上iOS:如何正確停止活動指示器?

-(void)method 
{ 
    ... 
    [ind performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:NO]; 
} 

它does'n工作這樣做。方法被正確調用,任何其他稱爲選擇器的工作,但不stopAnimating。 我把[ind stopAnimating]放在另一個函數中,然後通過performSelectorOnMainThread調用它,但它仍然沒有奏效。

+1

檢查這個答案異步執行的代碼: http://stackoverflow.com/a/9336253/986169。它可能有幫助 – giorashc

+0

確保在啓動後嘗試停止它。 :) 在多線程環境中,這是一個重要的東西。 – krafter

+0

非常好的提示,在答案 – user3290180

回答

3

您還可以使用它啓動和停止在一個單一的方法主線程活動指示燈下面的方法,還爲您提供良好的

- (void)showIndicatorAndStartWork 
{ 
    // start the activity indicator (you are now on the main queue) 
    [activityIndicator startAnimating]; 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     // do your background code here 

     dispatch_sync(dispatch_get_main_queue(), ^{ 
      // stop the activity indicator (you are now on the main queue again) 
     [activityIndicator stopAnimating]; 
     }); 
    }); 
} 
2

嘗試:

-(void)method 
{ 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [ind stopAnimating]; 
    }); 
} 
5

嘗試......

創建停止動畫

-(void)stopAnimationForActivityIndicator 
{ 
    [ind stopAnimating]; 
} 

的方法替換你的方法是這樣 -

-(void)method 
{ 
    ... 
    [self performSelectorOnMainThread:@selector(stopAnimationForActivityIndicator) withObject:nil waitUntilDone:NO]; 
} 

應做魔術...