如何在iPhone应用程序中加载Web视图的内容时显示加载图像

时间:2013-04-03 13:34:57

标签: iphone objective-c cocos2d-iphone

我有一个需要在网页视图中访问的网址,并且网址已成功访问。但问题是在webview中加载url需要花费更多时间,因此我需要在加载url时显示加载图像。我正在努力,但无法让它显示加载栏,加载图标或其他任何可能。

我的代码就在这里

 NSLog(@"Response ==> %@" ,encodedString);
 //'encodedstring' is my url which need to access

   // code to show a loading image
            indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
            indicator.frame = CGRectMake(0, 0, 320, 470);
            [self.view addSubview:indicator];
            [indicator release];
            [indicator startAnimating];

        UIWebView *webView;
        webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 470)];
        [webView setDelegate:self];

        NSString* urlTwo = [[encodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
                            stringByReplacingOccurrencesOfString:@"%22" withString:@""];

        NSURL *url2;
        url2 = [[NSURL alloc] initWithString:urlTwo];
        NSLog(@"url2:%@", url2);

        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];

        [webView loadRequest:requestObj];
        [[self view] addSubview:webView];

    }
    }


-(void)webViewDidFinishLoad:(UIWebView *)webView
{
[indicator stopAnimating];
[[self view] addSubview:webView];
}  

-(void)webViewDidStartLoad:(UIWebView *)webView
{
[indicator startAnimating];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[webView stopLoading];
}

任何人都可以在这里解决我的问题并让这一天快乐。提前谢谢。

1 个答案:

答案 0 :(得分:2)

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

    [indicator stopAnimating];
}
-(void)webViewDidStartLoad:(UIWebView *)aWebView
{
    [indicator startAnimating];
}

-(void)webView:(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
    [activityView stopAnimating];

}

你试过吗?