UIWebView
2015-11-10 11:44 xiangjune 阅读(204) 评论(0) 编辑 收藏 举报
UIWebView *myWebView;
UIButton *backBtn;
UIButton *goForwordBtn;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
backBtn = [UIButton buttonWithType:UIButtonTypeSystem];
backBtn.frame = CGRectMake(100, 100, 100, 50);
[backBtn setTitle:@"返回" forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
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];
}