问题
My viewController has a UIView property which contains a WKWebView. And I set the WKWebView scrollView delegate to my viewController, which is a public function of my UIView subClass and called in my viewController.
The issue is when I call [viewController popViewControllerAnimated]
, it will crash on [UIScrollView setDelegate:]
.
I have fixed the issue by add viewController.UIView.WKWebView.scrollView.delegate = nil;
in viewController's dealloc.
But why? WKWebView's dealloc is after viewController's dealloc, I suppose viewController is set to nil and dealloc in WKWebView will update its delegate to nil then cause BAD_ACCESS? But why dealloc will inplicit call setDelegate???
回答1:
In similar situation for WKWebView I had similar issue on assigning delegate to self. Implementing deinit resolved for me:
deinit {
webView.scrollView.delegate = nil
}
回答2:
For ObjC, setting the scrollView delegate to nil in dealloc
was still causing a crash.
Had to nil the delegate in didMoveToSuperview
- (void)didMoveToSuperview {
if (self.superview == nil) {
self.scrollView.delegate = nil;
}
}
These threads helped me
https://github.com/readium/r2-navigator-swift/pull/4
https://bugs.webkit.org/show_bug.cgi?id=159980
来源:https://stackoverflow.com/questions/38733634/ios-wkwebview-scrollview-delegate-cause-bad-access