首先創建UIWebView,然後定義前進、後退、刷新按鈕,並且需要實現UIWebViewDelegate方法
1.後退按鈕事件
-(IBAction)WebViewBack:(id)sender
{
if([self.webview canGoBack]){
[self.webview goBack];
}
}
2、前進按鈕事件
-(IBAction)WebViewForward:(id)sender
{
if([self.webview canGoForward])
[self.webview goForward];
}
3.刷新事件
-(IBAction)WebViewRefresh:(id)sender
{
[self.webview reload];
}
4.UIWebViewDelegate代理方法webViewDidFinishLoad
-(void)webViewDidFinishLoad:(UIWebView *)webView1
{
NSString *title = [webView1 stringByEvaluatingJavaScriptFromString:@"document.title"];
_webviewTitleLabel.text=title;
if(![webView1 canGoBack])
{
_btnWebviewGoBack.enabled=NO;
}
else
{
_btnWebviewGoBack.enabled=YES;
}
if(![webView1 canGoForward])
{
_btnWebviewGoForward.enabled=NO;
}
else
{
_btnWebviewGoForward.enabled=YES;
}//在webView加載完成後判斷當前是否可以前進或後退,以決定後退鍵和前進鍵是否可用
[_indicator stopRotateAnimation];//停止旋轉刷新的圖片
[_loadingView stopAnimating_LoadingView];//停止LoadingView的旋轉
}
5.UIWebViewDelegate代理方法webViewDidStartLoad
-(void)webViewDidStartLoad:(UIWebView *)webView
{
[_indicator startAnimating];//開始旋轉刷新的圖片
[_loadingView startAnimating_LoadingView];//開始LoadingView的旋轉
}