Mohd Shahid
Mohd Shahid

Reputation: 1606

MBProgressHUD indicator does not hide

i am able to show HUD indicator in viewDidLoad successfully but not able hide it in webViewDidFinishLoad method when webview is completely loaded. Please help.

i am using below code::

in .h file

MBProgressHUD *HUD;

in viewDidLoad

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *query = [[NSString alloc] initWithFormat:@"http://localhost/index.php?uid=%@", [[UIDevice currentDevice] uniqueIdentifier]];
    NSURL *url = [[NSURL alloc] initWithString:query];

    NSString *response = [[NSString alloc] initWithContentsOfURL:url];
    if(response)
    {
          [webView loadRequest:[NSURLRequest requestWithURL:url]];
    }
    else
    {
        //NSLog(@"err %@",response);
    }


    HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    HUD.delegate = self;
    HUD.labelText = @"loading";

}

and in webViewDidFinishLoad

- (void)webViewDidFinishLoad:(UIWebView *)web
{
    [HUD hide:TRUE]; //it does not work for me :(
}

Upvotes: 2

Views: 9279

Answers (4)

Crystian Leão
Crystian Leão

Reputation: 705

You should not call MBProgressHUD from viewDidLoad, try calling it from viewDidAppear and everything should work well.

Upvotes: 2

Hiren
Hiren

Reputation: 12780

try with this one

[HUD hide:YES];
if(HUD!=nil && [HUD retainCount]>0)
{ 
    [HUD removeFromSuperview];
    [HUD release];
    HUD=nil;
}

Upvotes: 3

Mohd Shahid
Mohd Shahid

Reputation: 1606

i have fixed the error, i moved the code from viewDidLoad to webViewDidStartLoad and everything is working fine this time :)

- (void)webViewDidStartLoad:(UIWebView *)web
{
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"loading";
}

- (void)webViewDidFinishLoad:(UIWebView *)web
{

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

Upvotes: 10

Cyprian
Cyprian

Reputation: 9453

Try removing it using this class method:

+ (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated 

.

- (void)webViewDidFinishLoad:(UIWebView *)web
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

If you use this method then you should think about rewriting your viewDidLoad this way:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //...

    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"loading";
}

Upvotes: 0

Related Questions