星五博客

UIWebView翻页返回效果(变通方法)

简单实现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;
    }
}

完整代码可到 http://code.cocoachina.com/detail/316925/UIWebView%E7%BF%BB%E9%A1%B5%E8%BF%94%E5%9B%9E%E6%95%88%E6%9E%9C%EF%BC%88%E5%8F%98%E9%80%9A%E6%96%B9%E6%B3%95%EF%BC%89/ 下载,已经上传了。

IOS