UIWebView原生跳转界面PUSH POP

来源:互联网 发布:域策略禁止安装软件 编辑:程序博客网 时间:2024/06/03 23:01

事情发生在CocoaChina论坛上,看到一个妹子遇到webview push出去后返回回来一片空白,于是就跟妹子要了链接.自己写个代码试了下.

    至于为什么空白我也没有想到原因,可能是webview的机制吧..

    哪位大神有更好的解决办法可以告诉我下


关于避免空白我是这么做的

写个NavigationController继承自UINavigationController

写个代理获取pop事件

////  BaseNavigationController.h//  Test////  Created by XuTianyu on 16/3/8.//  Copyright © 2016年 lakala. All rights reserved.//#import <UIKit/UIKit.h>@protocol UINavigationBackDelegate <NSObject>@optional/** *  点击返回按钮 * *  @param viewcontroller 返回按钮点击后的界面 */-(void)navigationBackButtonClickedWithViewController:(UIViewController *)viewcontroller;@end@interface BaseNavigationController : UINavigationController@property (weak, nonatomic)id<UINavigationBackDelegate>backDelegate;@end
////  BaseNavigationController.m//  Test////  Created by XuTianyu on 16/3/8.//  Copyright © 2016年 lakala. All rights reserved.//#import "BaseNavigationController.h"@implementation BaseNavigationController-(UIViewController *)popViewControllerAnimated:(BOOL)animated{    //获取当前viewControllers    NSArray *vcs = self.viewControllers;    //这个方法返回的是后退按钮所在的viewcontroller    UIViewController * viewController = [super popViewControllerAnimated:animated];    if ([self.backDelegate respondsToSelector:@selector(navigationBackButtonClickedWithViewController:)]) {        //获取当前viewcontroller的下标        NSInteger currentIndex = [vcs indexOfObject:viewController];        //获取前一个viewcontroller        UIViewController *backToVC = vcs[currentIndex - 1];        [self.backDelegate navigationBackButtonClickedWithViewController:backToVC];    }    return nil;}@end

然后在代理方法里面做下处理

-(void)navigationBackButtonClickedWithViewController:(UIViewController *)viewcontroller{    if ([viewcontroller isKindOfClass:[WebViewController class]]) {        //如果pop回来的界面是web,就reload下,解决空白问题        WebViewController *backViewController = (WebViewController *)viewcontroller;        [backViewController.webView reload];    }}



<pre name="code" class="objc">////  WebViewController.m//  Test////  Created by XuTianyu on 16/3/2.//  Copyright © 2016年 lakala. All rights reserved.//#import "WebViewController.h"@interface WebViewController ()<UIWebViewDelegate>@end@implementation WebViewController-(UIWebView *)webView{    if (!_webView) {        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];        _webView.delegate = self;        _webView.scalesPageToFit = YES;        _webView.contentMode = UIViewContentModeScaleAspectFit;            }    return _webView;}-(void)loadData{    //加载网页    NSURLRequest *request=[NSURLRequest requestWithURL:self.url];    [self.webView loadRequest:request];}- (void)viewDidLoad {    [super viewDidLoad];    [self loadData];     [self.view addSubview:self.webView];       // Do any additional setup after loading the view.}//获取网页标题-(void)webViewDidFinishLoad:(UIWebView *)webView{ self.title =  [webView stringByEvaluatingJavaScriptFromString:@"document.title"];}-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{    //如果跳转的网址跟当前界面生成时的网址一样 就不在本页面处理.push一个新的页面    if (![request.URL.absoluteString isEqualToString:self.url.absoluteString]) {        WebViewController *webviewVC = [[WebViewController alloc] init];        webviewVC.url =  request.URL;        [self.navigationController pushViewController:webviewVC animated:YES];        return NO;    }else        return  YES;}@end




0 0
原创粉丝点击