2

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 :(
}
Mohd Shahid
  • 1,538
  • 2
  • 33
  • 66
  • Give [SVProgressHUD](https://github.com/samvermette/SVProgressHUD) a try if you can't find a fix. In your case it would be as simple as calling `[SVProgressHUD showWithStatus:@"Loading"]` and then `[SVProgressHUD dismiss]` – samvermette Dec 26 '11 at 17:54
  • thanks, SVProgressHUD does exactly what i want but it is strange that it only works in xcode simulator and not on my ipad, what i am doing wrong? – Mohd Shahid Dec 26 '11 at 18:52
  • Try showing it in `viewDidAppear:` instead of `viewDidLoad`. – samvermette Dec 26 '11 at 19:06

4 Answers4

10

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];
}
Mohd Shahid
  • 1,538
  • 2
  • 33
  • 66
3

try with this one

[HUD hide:YES];
if(HUD!=nil && [HUD retainCount]>0)
{ 
    [HUD removeFromSuperview];
    [HUD release];
    HUD=nil;
}
Hiren
  • 12,720
  • 7
  • 52
  • 72
2

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

Crystian Leão
  • 695
  • 1
  • 8
  • 18
0

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";
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73