UI中的網頁加載

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.title = @"圖片新聞";

    //新建網頁視圖

    webView = [[UIWebView alloc]initWithFrame:self.view.bounds];

    //網頁視圖自適應屏幕尺寸

    webView.scalesPageToFit = YES;

    //設置代理

    webView.delegate = self;

    [self.view addSubview:webView];

    

    //加載百度

//    //創建request對象

//    NSMutableURLRequest *mResquest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];

//    //網頁控件加載一個

//    [webView loadRequest:mResquest];

    

    //初始化風火輪

    UIActivityIndicatorView *ac = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];

    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:ac];

    self.navigationItem.rightBarButtonItem = rightItem;

    [self _loadData];

}

- (void)_loadData{

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"news" ofType:@"html"];

    NSError *error = nil;

    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];

    NSDictionary *jsonData = [ReturnJson showJson:news_detail];

    NSString *title = [jsonData objectForKey:@"title"];

    NSString *source = [jsonData objectForKey:@"source"];

    NSString *time = [jsonData objectForKey:@"time"];

    NSString *author = [jsonData objectForKey:@"author"];

    NSString *content = [jsonData objectForKey:@"content"];

    NSString *htmlStr = [NSString stringWithFormat:str,title,source,time,content,author];

    [webView loadHTMLString:htmlStr baseURL:nil];

}

#pragma mark-UIWebViewDelegate

//開始加載

- (void)webViewDidStartLoad:(UIWebView *)webView{

    UIActivityIndicatorView *ac = (UIActivityIndicatorView *)self.navigationItem.rightBarButtonItem;

   [ac startAnimating];

}

//結束加載

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

    UIActivityIndicatorView *ac = (UIActivityIndicatorView *)self.navigationItem.rightBarButtonItem;

    [ac stopAnimating];

}

//加載出錯

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章