ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController :UIViewController<UIWebViewDelegate>//加載聲明UIWebviewDelegate協議
{
UIWebView *webView;
UIActivityIndicatorView *activityIndicator;
}
ViewController.m文件
//加載網頁
- (void)viewDidLoad
{
[superviewDidLoad];
webView = [[UIWebViewalloc] initWithFrame:CGRectMake(0,0, 320,480)];
[webViewsetDelegate:self];
NSURLRequest *request =[NSURLRequestrequestWithURL:[NSURLURLWithString:@"http://www.zqksk.com/ios/xzqcf/index.html"]];
[self.viewaddSubview: webView];
[webViewloadRequest:request];
}
- (void) webViewDidStartLoad:(UIWebView *)webView
{
//創建UIActivityIndicatorView背底半透明View
UIView *view = [[UIViewalloc] initWithFrame:CGRectMake(0,0, 320, 480)];
[viewsetTag:108];
[view setBackgroundColor:[UIColorblackColor]];
[viewsetAlpha:0.5];
[self.viewaddSubview:view];
activityIndicator = [[UIActivityIndicatorViewalloc] initWithFrame:CGRectMake(0.0f,0.0f, 32.0f, 32.0f)];
[activityIndicatorsetCenter:view.center];
[activityIndicatorsetActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[view addSubview:activityIndicator];
[activityIndicatorstartAnimating];
NSLog(@"webViewDidStartLoad");
}
/////數據加載完
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicatorstopAnimating];
UIView *view = (UIView*)[self.viewviewWithTag:108];
[view removeFromSuperview];
NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[activityIndicatorstopAnimating];
UIView *view = (UIView*)[self.viewviewWithTag:108];
[view removeFromSuperview];
NSLog(@"didFailLoadWithError:%@", error);
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (myAlert==nil){
myAlert = [[UIAlertViewalloc] initWithTitle:nil
message: @"正在玩命加載當中..."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
UIActivityIndicatorView *activityView = [[UIActivityIndicatorViewalloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];//加載時指示器的顏色
activityView.frame =CGRectMake(120.f,48.0f, 37.0f,37.0f);//設置對象的位置,大小是固定不變的。WhiteLarge爲37 * 37,White爲20 * 20
activityView.color = [UIColor redColor];//指示器的顏色
[myAlertaddSubview:activityView];//將對像加入到VIEW
[activityViewrelease];////要記得將對象release
[activityViewstartAnimating];//開啓動畫(指示器)
[myAlertshow];
}
_reloading = YES;
}
////數據加載完
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[myAlertdismissWithClickedButtonIndex:0animated:YES];
_reloading = NO;
[_refreshHeaderViewegoRefreshScrollViewDataSourceDidFinishedLoading:self.uiWebView.scrollView];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"load page error:%@", [errordescription]);
_reloading = NO;
[_refreshHeaderViewegoRefreshScrollViewDataSourceDidFinishedLoading:self.uiWebView.scrollView];
}
轉:
http://justcoding.iteye.com/blog/1535070
http://lijinfengjava.iteye.com/blog/1508377
http://blog.sina.com.cn/s/blog_7fa6b06f010121mr.html