2014-02-19 87 views
2

我正在使用MBProgressHUD自定義控件顯示從Web加載JSON數據時的情況。MBProgressHUD未在iOS中顯示

我已經在他們的視圖中找到很多不能正確顯示HUD控制的答案。

這是我的代碼在我的視圖中顯示HUD

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

以下是我如何隱藏我的HUD視圖。

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

在ViewDidLoad中,該控件工作正常。

但是,當我點擊刷新按鈕,並希望顯示HUD控制時,它不顯示HUD控制。

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

dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL); 
dispatch_async(myqueue, ^{ 

    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:nil waitUntilDone:YES]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tbl reloadData]; 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    }); 
}); 

我不知道我做錯了什麼?請幫幫我。

+1

即使你做了一堆調度,你所有的工作都在主隊列上結束!這將阻止用戶界面,HUD不會顯示。 – Jack

+0

那麼,如何編輯我的代碼? –

+0

嘗試調用fetchedData:直接而不是在主線程上執行,看看是否一切仍然有效 – Jack

回答

9

代碼切換到這一點:

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

dispatch_queue_t myqueue = dispatch_queue_create("queue", NULL); 
dispatch_async(myqueue, ^{ 

    //Whatever is happening in the fetchedData method will now happen in the background 
    [self fetchedData:nil]; 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     [self.tbl reloadData]; 
     [MBProgressHUD hideHUDForView:self.view animated:YES]; 
    }); 
}); 

你不想調用主線程上fetchData方法。如果您使用上面的代碼,則主線程上將不會出現fetchedData方法,因此請確保您不更新UI或其中的任何內容。

只是一個建議,我不會使用名稱"queue"爲您的dispatch_queue。應用程序中的隊列名稱必須是唯一的,所以我會將它稱爲"your.bundle.id.viewcontrollername",以避免稍後出現問題。

+0

謝謝你兄弟。現在我用你的代碼得到了它。 :) –