简单实现UIWebView翻页返回效果
原理就是两个ViewController互跳,处理下UIWebView的shouldStartLoadWithRequest来控制跳转;
利用Navigation Controller实现跳转和手势处理,跳转时把需要访问的url传过去,也就是两个ViewController之间互相传来传去。
部分代码如下:
@implementation A
@synthesize objUrl;
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.automaticallyAdjustsScrollViewInsets=NO;//防止滚动类头部出现空行
if ([[[UIDevicecurrentDevice] systemVersion] floatValue] >= 7.0) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
}
NSURLRequest *request=[NSURLRequestrequestWithURL:[NSURLURLWithString:objUrl]];
//NSLog(@"网址:%@",objUrl);
[webViewsetDataDetectorTypes:UIDataDetectorTypePhoneNumber];
[webViewsetUserInteractionEnabled:YES]; //是否支持交互
[webViewloadRequest:request];
[self.viewaddSubview: webView];
[webViewsetDelegate:self];
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if(navigationType==UIWebViewNavigationTypeLinkClicked) {//判断是否是点击链接
UIStoryboard *story = [UIStoryboardstoryboardWithName:@"Main"bundle:[NSBundlemainBundle]];
UIViewController *myView = [story instantiateViewControllerWithIdentifier:@"B"];
[myView setValue:request.URL.absoluteStringforKey:@"objUrl"];
[self.navigationControllerpushViewController:myView animated:YES];
returnNO;
} else {
returnYES;
}
}