如何在加载请求时停止WKWebView

时间:2016-11-23 06:58:42

标签: ios objective-c wkwebview

我的UIViewController方法viewDidLoad如下:

self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64)];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.passageUrl]];
[self.webView loadRequest:request];
self.webView.UIDelegate = self;
self.webView.navigationDelegate = self;
[self.view addSubview:self.webView];

现在我想在加载完成之前执行popViewController操作,因为有时Internet连接不好,我在其pop动作中使用下面的代码,代码立即执行但是webView无法立即停止加载,在控制器弹出之前几秒钟,任何人都对如何解决这个问题有所了解?

- (void)doBack:(id)sender {
    [self.webView stopLoading];
    [self.navigationController popViewControllerAnimated:YES];
}

我发现在模拟器中效果很好,事实证明问题只存在于设备中。

1 个答案:

答案 0 :(得分:0)

试试这个

- (id)popToViewControllerWithClassname:(NSString *)className Animated:(BOOL)animated {
    Class class =  NSClassFromString(className);
    for (UIViewController *controller in self.viewControllers) {
        if([controller isKindOfClass:class]) {
            [self popToViewController:controller animated:animated];
            return controller;
        }
    }
    return nil;
}

通话方法

[self.navigationController popToViewControllerWithClassname:@"TestViewController" Animated:YES];