2012-03-24 108 views
0

我正在玩MBProgressHUD。在的ViewController(tableview中)的worflow是這樣認爲:MBProgressHUD不會顯示

1)IBAction爲被調用,以2頁不同的表 2.1)功能reloadAllMonth之間切換被調用(初始化與新表中的數據) 2.2)MBProgressHUD陣列* HUD應顯示 3)reloadAllMonth完成 4)HUD應該消失

我當前的代碼:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ 

     //sparing you the code that determins where the tap occured 

     HUD = [[MBProgressHUD alloc]initWithFrame:self.view.frame]; 
     [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
     [self reloadAllMonth]; 
     allMonthIsActive = YES; 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 

// some other code table reload etc. 
} 

會發生什麼:

- >功能的touchesBegan被稱爲 - >什麼也沒有發生3-4秒(加載時間) - >新表彈出

問題:爲何HUD顯示不出來?我哪裏做錯了 ?

回答

2

你不能同時擁有顯示和隱藏在同一個線程,這是我會做什麼:

-(void)reloadStuff 
{ 
    [self reloadAllMonth]; 
    allMonthIsActive = YES; 
    [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    isLoading = NO; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    //sparing you the code that determins where the tap occured 

    if (isLoading) 
     return; 
    isLoading = YES; 
    [MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
    [self performSelectorInBackground:@selector(doStuff) withObject:nil]; 
} 

您也可以刪除伊娃HUD,你不使用它。

而且不要忘記在viewDidLoad中初始化isLoading爲NO:

isLoading = NO; 

祝你好運!

+0

我改變了我的代碼給你的建議,但它沒有改變任何東西。 HUD仍然不會顯示。我更新了問題代碼到新的。 – 2012-03-24 14:47:19

+0

回答評論 – Zalykr 2012-03-24 15:25:26

+0

這是我絕對的第一個想法和代碼。問題是:reloadAllMonth方法然後在backgroundthread中執行。因此主線程運行並開始繪製tableview。 tableview需要我在reloadAllMonth中加載的數據。所以表開始繪製數據之前,因此它的應用程序崩潰=) – 2012-03-24 15:45:17

0

你是否從服務器獲取任何東西? 如果是這樣,請檢查您是否正在傳遞同步請求或異步。異步請求是最好的。 以這種方式處理MBProgressBar。

[MBProgressHUD showHUDAddedTo:self.view animated:YES]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      //Any UI updates should be made here . 
       [MBProgressHUD hideHUDForView:self.view animated:YES]; 
     });