2

当 webview 完全加载时,我能够在 viewDidLoad 中成功显示 HUD 指示器,但无法在 webViewDidFinishLoad 方法中隐藏它。请帮忙。

我正在使用以下代码::

在 .h 文件中

MBProgressHUD *HUD;

在 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";

}

并在 webViewDidFinishLoad

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

4 回答 4

10

我已经修复了错误,我将代码从viewDidLoad移动到webViewDidStartLoad,这次一切正常:)

- (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];
}
于 2011-12-27T10:32:37.750 回答
3

试试这个

[HUD hide:YES];
if(HUD!=nil && [HUD retainCount]>0)
{ 
    [HUD removeFromSuperview];
    [HUD release];
    HUD=nil;
}
于 2011-12-27T10:35:59.037 回答
2

您不应该调用MBProgressHUDfrom viewDidLoad,尝试调用它 from viewDidAppear,一切都应该运行良好。

于 2013-05-10T04:14:00.790 回答
0

尝试使用此类方法将其删除:

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

.

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

如果您使用此方法,那么您应该考虑以这种方式重写您的 viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    //...

    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"loading";
}
于 2011-12-26T19:46:47.097 回答