UIWebView网页视图

来源:互联网 发布:ubuntu kylin社区 编辑:程序博客网 时间:2024/06/05 15:19

screenshot.png

#import"ViewController.h"

@interfaceViewController()<UIWebViewDelegate>
@property(retain)UIWebView* webView;
@end

@implementationViewController

- (
void)viewDidLoad {
    [
superviewDidLoad];
   
// Do any additional setup after loading the view, typically from a nib.
   
//按钮上一页还有刷新
    [
selfMyButton];
   
   
//创建网页视图
    [
selfWebpage];
}

-(
void)Webpage
{
   
//创建网页视图
   
_webView = [[UIWebViewalloc]initWithFrame:CGRectMake(0,0, [UIScreenmainScreen].bounds.size.width, [UIScreenmainScreen].bounds.size.height)];
   
_webView.backgroundColor= [UIColorredColor];
    [
self.viewaddSubview:_webView];
   
//设置代理
   
_webView.delegate=self;
   
   
//创建百度的url链接
   
NSURL* url = [NSURLURLWithString:@"https://www.baidu.com"];
   
//创建URL请求
   
NSURLRequest* request = [NSURLRequestrequestWithURL:url];
   
//webView开始请求数据
    [
_webViewloadRequest:request];
}

-(
void)MyButton
{
   
//上一页
   
self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"上一页"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(leftAction:)];
   
//刷新
   
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"刷新"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(rightAction:)];
}

-(
void)leftAction:(UIBarButtonItem*)Myleft
{
    [
_webViewgoBack];
}

-(
void)rightAction:(UIBarButtonItem*)Myright
{
    [
_webViewreload];
}

#pragma mark- UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView*)webView
{
   
NSLog(@"开始请求");
}

- (
void)webViewDidFinishLoad:(UIWebView*)webView
{
   
NSLog(@"请求成功结束");
}

- (
void)webView:(UIWebView*)webView didFailLoadWithError:(NSError*)error
{
   
NSLog(@"请求失败");
   
NSLog(@"error:%@",error);
}

- (
void)didReceiveMemoryWarning {
    [
superdidReceiveMemoryWarning];
   
// Dispose of any resources that can be recreated.
}


@end

原创粉丝点击