I have an tabbed App using UITabBarController
. There are 2 tabs, linked to ViewController1
& ViewController2
respectively. When each view controller loads, it will fetch data from server. Therefore I use MBProgressHUD
to show loading status.
However, the behavior currently acts like this: When I switch tab, the app looks like hanged. But after the data is loaded from server, MBProgressHUD
flashes a second then disappear, then the view is changed to new tab.
But what I want is: MBProgressHUD
shows when the new tab is once touched, and the MBProgressHUD
dismisses when the data finishes loading. What did I miss?
Here is the code I use:
- (void)viewDidLoad
{
[super viewDidLoad];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = NSLocalizedString(@"Loading", nil);
// load data from server, parsing XML, etc
[MBProgressHUD hideHUDForView:self.view animated:YES];
}
UPDATE: Here I have a sample xCode project , you guys can give it a try and tell me what did I miss. [ Download Link ]
I have had this same issue before. The HUD needs access to the main thread to allow it to show. My guess is you are calling it in a nested method that is blocking out the main thread.
Try calling a method that calls your workload on the main thread. This will present your HUD and dismiss when needed.
[HUD showWhileExecuting:@selector(callingMethod) onTarget:self withObject:nil animated:YES];
-(void)callingMethod {
[self performSelectorOnMainThread:@selector(dataLoadMethod) withObject:nil waitUntilDone:NO];
}