문제

MPProgressView won't display when I try to push a viewcontroller until seconds before the pushed VC is displayed. Should the viewController be placed in the same function as the MBProgressView is displayed? I've made sure that my MBProgressView is on the main thread, I've tried many solutions on SO and can't see anyone with the same issue. I am simply trying to display the MBProgressHUD while the viewController is loading and being pushed. Thanks!

I am using MBProgressView as follows:

- (IBAction)pushButton:(id)sender
{

    self.HUD =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view addSubview:self.HUD];
    self.HUD.labelText = @"Doing stuff...";
    self.HUD.detailsLabelText = @"Just relax";
    self.HUD.delegate=self;

      [self.view addSubview:self.HUD];
   [self.HUD showWhileExecuting:@selector(loadCreate) onTarget:self withObject:nil animated:YES];



}


- (void)loadCreate {

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


-(void)dataLoadMethodMail
{NSLog(@"data load method is displaying");


   SelectViewController *mvc = [[SelectViewController alloc] init];
   [self.navigationController pushViewController:mvc animated:YES];


}
도움이 되었습니까?

해결책

You don't need to add self.HUD to self.view, showHUDAddedTo: does it for you.

[self.HUD showWhileExecuting:@selector(loadCreate) onTarget:self withObject:nil animated:YES];

Shows the hud until loadCreate returns.

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

dispatches something on main thread and returns right after (before the actual end of dataLoadMethodMail). The HUD is shown but disappears right away.

To solve the issue try hiding manually the HUD when dataLoadMethodMail finishes it's work.

Just replace

 [self.HUD showWhileExecuting:@selector(loadCreate) onTarget:self withObject:nil animated:YES];

with

[self loadCreate];

and add

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

at the end of dataLoadMethodMail

PS : Loading data should not be done on main thread.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top