UIWebView

 

UIWebView *myWebView;html

UIButton *backBtn;web

UIButton *goForwordBtn;ide

 

- (void)viewDidLoad {url

    [super viewDidLoad];orm

    // Do any additional setup after loading the view, typically from a nib.htm

    

    backBtn = [UIButton buttonWithType:UIButtonTypeSystem];get

    backBtn.frame = CGRectMake(100, 100, 100, 50);string

    [backBtn setTitle:@"返回" forState:UIControlStateNormal];it

    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];io

    backBtn.enabled = NO;

    [self.view addSubview:backBtn];

    

    goForwordBtn = [UIButton buttonWithType:UIButtonTypeSystem];

    goForwordBtn.frame = CGRectMake(250, 100, 100, 50);

    [goForwordBtn setTitle:@"前進" forState:UIControlStateNormal];

    [goForwordBtn addTarget:self action:@selector(goForword) forControlEvents:UIControlEventTouchUpInside];

    goForwordBtn.enabled = NO;

    [self.view addSubview:goForwordBtn];

    

    myWebView = [[UIWebView alloc]initWithFrame:CGRectMake(100, 160, 500, 400)];

    myWebView.scalesPageToFit = YES;

    myWebView.delegate = self;

    

    // 1.加載網頁地址

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

//    [myWebView loadRequest:request];

    

    // 2.加載本地地址

    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];

    NSURL *url = [NSURL URLWithString:path];

    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    

    [myWebView loadHTMLString:content baseURL:url];

    

    

    [self.view addSubview:myWebView];

    

    

}

 

-(void)goForword

{

    [myWebView goForward];

}

-(void)back

{

    [myWebView goBack];

}

 

-(void)refreshBtnStatus

{

    if([myWebView canGoBack])

    {

        backBtn.enabled = YES;

    }

    else

    {

        backBtn.enabled = NO;

    }

    

    if([myWebView canGoForward])

    {

        goForwordBtn.enabled = YES;

    }

    else

    {

        goForwordBtn.enabled = NO;

    }

}

// WEBVIEW加載完成調用。跳轉頁面完成也調用了該方法

- (void)webViewDidFinishLoad:(UIWebView *)webView

{

    NSLog(@"webViewDidFinishLoad");

    [self refreshBtnStatus];

}

相關文章
相關標籤/搜索