0

我有一個方法從通訊簿獲取聯繫人並與他們做一些東西(「getContactInformation」)在方法持續時間內顯示MBProgressHUD

這個過程有點長(幾秒),並在此過程後,我顯示一個新的ViewController。爲了使用戶友好,我想使用MBProgressHUD在過程開始時顯示一個活動指示器,並在最後隱藏它。

這是最好的辦法嗎?我已經測試這一點:

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

[self getContactInformation]; 

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

[self presentViewController:newController animated:NO completion:nil]; 

它不工作(它不顯示的指標)。任何人都可以幫助我?

預先感謝您

回答

1

保持獨立的方法[self presentViewController:newController animated:NO completion:nil];。並嘗試在某些延遲後調用該方法。這應該解決問題。

1
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
    [self getContactInformation]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [MBProgressHUD hideHUDForView:self.mapView animated:YES]; 
    }); 
}); 

這將產生一個線程來運行getContactInformation和將不會阻止這是需要做的UI的變化(如顯示HUD)主線程。

我猜測主線程在獲取聯繫信息時被鎖定,並且沒有時間更新以顯示HUD。方法完成後,它將再次獲取主線程以更新UI(刪除HUD)。