2012-02-11 104 views
4

後不消失,我相信這是我的iOS/ObjC小白岬的一個問題...MBProgressHUD調用隱藏

我有在項,當用戶選擇一個排一個UITableView,

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
... 
    [self.twitter sendUpdate:[self getTweet]]; 

- (NSString *)sendUpdate:(NSString *)text; 
{ 
    NSLog(@"showing HUD"); 
    self.progressSheet = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] keyWindow] animated:YES]; 
    self.progressSheet.labelText = @"Working:"; 
    self.progressSheet.detailsLabelText = text; 

    // Build a twitter request 
    TWRequest *postRequest = [[TWRequest alloc] initWithURL: 
           [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] 
               parameters:[NSDictionary dictionaryWithObject:text 
                         forKey:@"status"] requestMethod:TWRequestMethodPOST]; 

    // Post the request 
    [postRequest setAccount:self.twitterAccount]; 

    // Block handler to manage the response 
    [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
    { 
     NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]); 

      NSLog(@"hiding HUD"); 
      [MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES]; 
      self.progressSheet = nil; 

它調用內置的Twitter API發送鳴叫。在發送過程中,我正在使用MBProgressHUD。 HUD消失後,我的行爲越來越不穩定,通常它會比應該延長10秒左右。根據我看到的顯示/隱藏日誌記錄。

我有另一個更簡單的視圖,它只是列出推文並且使用HUD沒有問題 - 儘管它通過viewWillAppear調用完成。

也許我需要通過另一個線程來顯示?

預先感謝任何想法〜克里斯

+1

爲什麼把你的平視顯示器安裝到窗戶上?將它附加到您的視圖更有意義。此外,然後,而不是使用hideHUDforView方法爲什麼不只是調用hide:在您的propertySheet HUD上動畫,您已經有一個參考?這會提供幫助。 – peterept 2012-02-11 12:06:53

+0

謝謝 - 就像它在一些常見的代碼中一樣,我避免傳遞視圖。我曾嘗試過,但沒有幫助。試圖切換到使用hide:call,但沒有更好的。我在稍後的didSelectRow調用中進行了一些GC調用,但添加了一些日誌記錄顯示,這不是阻塞。 – 2012-02-11 17:17:32

回答

5

看來我的問題是,我試圖關閉HUD的主線以外的其他線程。

在這個問題的答案之一中使用這個技巧,它現在工作得更好。

GCD to perform task in main thread

也就是說,使用規定 「runOnMainQueueWithoutDeadlocking」 的方法

的密切對話的代碼是現在這個樣子:

runOnMainQueueWithoutDeadlocking(^{ 
    NSLog(@"hiding HUD/mainthread"); 
    [self.progressSheet hide:YES]; 
    self.progressSheet = nil; 
}); 
3

嘗試此方法用於顯示HUD:

MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
hud.labelText = @"Working"; 

相反的:

self.progressSheet = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication] keyWindow] animated:YES]; 
self.progressSheet.labelText = @"Working:"; 
self.progressSheet.detailsLabelText = text; 

而這個隱藏它:

[MBProgressHUD hideHUDForView:self.view animated:YES]; 

In代替:

[MBProgressHUD hideHUDForView:[[UIApplication sharedApplication] keyWindow] animated:YES]; 
+1

謝謝,但我曾嘗試失敗。我似乎通過將HUD隱藏在「主線程」上來修復它 - 請參閱下面的答案。 – 2012-02-11 23:02:03

+0

Thnak你其工作正常 – vijay 2016-02-27 07:43:55

11

是的,你說得對UI線程。 你也可以這樣寫:

dispatch_async(dispatch_get_main_queue(), ^{ 
    [self.progressSheet hide:YES]; 
    self.progressSheet = nil; 
});