UIWebView页面的控制

1.UIWebView的内容控制的属性/方法列表

loading属性 确认当前页面是否在读入中

canGoForward属性 确认goForward 方法是否可执行,可执行为yes;

canGoBack属性 确认goBack 方法是否可执行,可执行为yes;

goBack方法 返回前一个页面

goForword方法 进入下一个页面

reload方法 重新读入当前页

stopLoading方法 中止当前页的读入

2.webViewController.h

01. @interface webViewController : UIViewController<UIWebViewDelegate,UINavigationControllerDelegate,UINavigationBarDelegate>
02. {
03. UIWebView *_webView;
04. }
05.  
06. @property(nonatomic,strong)UIBarButtonItem *reloadButton;
07. @property(nonatomic,strong)UIBarButtonItem *stopButton;
08. @property(nonatomic,strong)UIBarButtonItem *backButton;
09. @property(nonatomic,strong)UIBarButtonItem *forwardButton;
10. @end


webViewController.m

01. - (void)viewDidLoad
02. {
03. [super viewDidLoad];
04. // Do any additional setup after loading the view.
05. self.navigationItem.title = @"UIWebView测试版";
06. _webView = [[UIWebView alloc]init];
07. _webView.delegate = self;
08. _webView.frame = self.view.frame;
09. _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
10. _webView.scalesPageToFit = YES;
11. [self.view addSubview:_webView];
12. //工具条中追加按钮
13. _reloadButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadDidPush)];
14. _stopButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action: @selector(stopDidPush)];
15. _backButton = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleBordered target:self action:@selector(backDidPush)];
16. _forwardButton = [[UIBarButtonItem alloc]initWithTitle:@"Forward"style:UIBarButtonItemStyleBordered target:self action:@selector(forwardDidPush)];
17. NSArray *buttons = [NSArray arrayWithObjects:_backButton,_forwardButton,_reloadButton,_stopButton, nil];
18. [self setToolbarItems:buttons animated:YES];
19. [self.navigationController setToolbarHidden:NO animated:YES];
20.  
21.  
22. }
23. -(void)reloadDidPush
24. {
25. [_webView reload];//重新读入页面
26. }
27. -(void)stopDidPush
28. {
29. if(_webView.loading){
30. [_webView stopLoading];//读入停止
31. }
32.  
33. }
34. -(void)backDidPush
35. {
36. if (_webView.canGoBack) {
37. [_webView goBack];//返回前一画面
38. }
39. }
40. -(void)forwardDidPush
41. {
42. if (_webView.canGoForward) {
43. [_webView goForward];//进入下一页面
44. }
45.  
46. }
47.  
48. -(void)updateControlEnabled
49. {
50. //统一更新指示按钮状态
51. [UIApplication sharedApplication].networkActivityIndicatorVisible = _webView.loading;
52. _stopButton.enabled = _webView.loading;
53. _backButton.enabled = _webView.canGoBack;
54. _forwardButton.enabled = _webView.canGoForward;
55.  
56. }
57. -(void)viewDidAppear:(BOOL)animated
58.  
59. {
60. //画面显示结果后读入web页面画面
61. [super viewDidAppear:animated];
62. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
63. [_webView loadRequest:request];
64. [self updateControlEnabled];
65. }
66. -(void)viewWillDisappear:(BOOL)animated
67. {
68. //画面关闭时状态的活动指示器设置成off
69. [super viewWillDisappear:animated];
70. [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
71. }
72.  
73. -(void)webViewDidStartLoad:(UIWebView *)webView{
74. [self updateControlEnabled];
75. }
76.  
77. -(void)webViewDidFinishLoad:(UIWebView *)webView
78.  
79. {
80. [self updateControlEnabled];
81. }
82. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
83. {
84. [self updateControlEnabled];
85. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值