codeTools

2038-加载 WebView

2017-03-04  本文已影响2人  ArrQing

1 新建一个 controller
2 实现 UIWebViewDelegate
3 成员变量 webView activityIndicator
4 根据自己的 需要 初始化 webView

#import "SecondDetailsViewController.h"

@interface SecondDetailsViewController ()<UIWebViewDelegate>{

    UIWebView *webView;
    UIActivityIndicatorView *activityIndicator;
}

@end

@implementation SecondDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getWebView];
}


- (void)getWebView{
// 协议
    webView.delegate = self;
// 初始化
    webView= [[UIWebView alloc] initWithFrame:CGRectMake(0, 150, 320, 150)];
// 请求地址
    NSURLRequest *request1 =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://114.215.108.58/stockInfo.html?code=sh603028"]];
    [webView loadRequest:request1];
//  自适应
    [webView setScalesPageToFit:YES];

    [self.view addSubview: webView];

    
    webView.delegate = self;
    webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 310, 320, 150)];
    NSURLRequest *request2 =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://114.215.108.58/jscharts.html?code=sh603028"]];
    [webView setScalesPageToFit:YES];
    [self.view addSubview: webView];
    [webView loadRequest:request2];
    
}
#pragma mark - webView

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
    [view setTag:108];
    [view setBackgroundColor:[UIColor blackColor]];
    [view setAlpha:0.1];
    [self.view addSubview:view];
    
    activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];
    [activityIndicator setCenter:view.center];
    [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
    [view addSubview:activityIndicator];
    
    [activityIndicator startAnimating];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    [activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
    
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [activityIndicator stopAnimating];
    UIView *view = (UIView*)[self.view viewWithTag:108];
    [view removeFromSuperview];
}

@end
上一篇下一篇

猜你喜欢

热点阅读