1.UIWebView的内容控制的属性/方法列表
loading属性 确认当前页面是否在读入中
canGoForward属性 确认goForward 方法是否可运行,可运行为yes;
canGoBack属性 确认goBack 方法是否可运行,可运行为yes。
goBack方法 返回前一个页面
goForword方法 进入下一个页面
reload方法 又一次读入当前页
stopLoading方法 中止当前页的读入
2.webViewController.h
@interface webViewController : UIViewController<UIWebViewDelegate,UINavigationControllerDelegate,UINavigationBarDelegate> { UIWebView *_webView; } @property(nonatomic,strong)UIBarButtonItem *reloadButton; @property(nonatomic,strong)UIBarButtonItem *stopButton; @property(nonatomic,strong)UIBarButtonItem *backButton; @property(nonatomic,strong)UIBarButtonItem *forwardButton; @end
webViewController.m
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.navigationItem.title = @"UIWebView測试版"; _webView = [[UIWebView alloc]init]; _webView.delegate = self; _webView.frame = self.view.frame; _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth; _webView.scalesPageToFit = YES; [self.view addSubview:_webView]; //工具条中追加按钮 _reloadButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadDidPush)]; _stopButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action: @selector(stopDidPush)]; _backButton = [[UIBarButtonItem alloc]initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(backDidPush)]; _forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"Forward" style:UIBarButtonItemStyleBordered target:self action:@selector(forwardDidPush)]; NSArray *buttons = [NSArray arrayWithObjects:_backButton,_forwardButton,_reloadButton,_stopButton, nil]; [self setToolbarItems:buttons animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; } -(void)reloadDidPush { [_webView reload];//又一次读入页面 } -(void)stopDidPush { if(_webView.loading){ [_webView stopLoading];//读入停止 } } -(void)backDidPush { if (_webView.canGoBack) { [_webView goBack];//返回前一画面 } } -(void)forwardDidPush { if (_webView.canGoForward) { [_webView goForward];//进入下一页面 } } -(void)updateControlEnabled { //统一更新指示按钮状态 [UIApplication sharedApplication].networkActivityIndicatorVisible = _webView.loading; _stopButton.enabled = _webView.loading; _backButton.enabled = _webView.canGoBack; _forwardButton.enabled = _webView.canGoForward; } -(void)viewDidAppear:(BOOL)animated { //画面显示结果后读入web页面画面 [super viewDidAppear:animated]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]]; [_webView loadRequest:request]; [self updateControlEnabled]; } -(void)viewWillDisappear:(BOOL)animated { //画面关闭时状态的活动指示器设置成off [super viewWillDisappear:animated]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; } -(void)webViewDidStartLoad:(UIWebView *)webView{ [self updateControlEnabled]; } -(void)webViewDidFinishLoad:(UIWebView *)webView { [self updateControlEnabled]; } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self updateControlEnabled]; }