#import "SAWebViewCtrl.h"
#import "UIBarButtonItem+SA.h"
@interface
SAWebViewCtrl () <UIWebViewDelegate>
@property
(
nonatomic
, strong) UIWebView *webView;
@property
(
nonatomic
, strong) UIBarButtonItem *backItem;
@property
(
nonatomic
, strong) UIBarButtonItem *closeItem;
@end
@implementation
SAWebViewCtrl
- (
void
)viewDidLoad
{
[
super
viewDidLoad];
self
.navigationItem.title =
@"WebView"
;
self
.webView = [[UIWebView alloc] init];
self
.webView.delegate =
self
;
self
.webView.frame =
self
.view.bounds;
self
.webView.autoresizingMask = 255;
[
self
.view addSubview:
self
.webView];
self
.backItem = [UIBarButtonItem backButtonWithTarget:
self
action:
@selector
(backEven:)];
self
.closeItem = [[UIBarButtonItem alloc] initWithTitle:
@"关闭"
style:UIBarButtonItemStylePlain target:
self
action:
@selector
(popPage)];
[
self
setLeftBarButton];
[
self
.webView loadRequest:[
NSURLRequest
requestWithURL:url]];
}
#pragma mark - 网页代码方法
- (
void
)webViewDidStartLoad:(UIWebView *)webView
{
[
self
setLeftBarButton];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:
YES
];
}
- (
void
)webViewDidFinishLoad:(UIWebView *)webView
{
[
self
setLeftBarButton];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:
NO
];
}
#pragma mark - 事件响应
- (
void
)backEven:(UIButton *)button
{
if
([
self
.webView canGoBack]){
[
self
.webView goBack];
}
else
{
[
self
popPage];
}
}
- (
void
)popPage
{
[
self
.navigationController popViewControllerAnimated:
YES
];
}
- (
void
)setLeftBarButton
{
if
([
self
.webView canGoBack]){
[
self
.navigationItem setLeftBarButtonItems:@[
self
.backItem,
self
.closeItem]];
}
else
{
[
self
.navigationItem setLeftBarButtonItems:@[
self
.backItem]];
}
}
@end