我遇到了 WKWebview 和导航委托的问题。 如果我在没有断点的情况下执行应用程序,则永远不会调用委托。如果我在创建 webview 之后在某处设置了断点,并且调用了 loadHtmlString 方法,那么委托就会被正确调用。
以下是我正在做的事情的简化版本:
//This custom view is part of a ViewController where the user interface was designed
// using the storyboardeditor
class CustomContainerView:UIView{
@IBOutlet weak var viewContainer: UIView!
public func render(){
let behaviour: IViewBehaviour = self.getBehaviour(slice) //In this case, the behaviour is an instance of the class BehaviourN3
let view = behaviour.getView() //In this case the returned view is a WKWebView
viewContainer.addSubview(view)
view.bindFrameToSuperviewBounds(0)
behaviour.render()
}
}
}
public class BeahviourN3: BaseBehaviour, WKNavigationDelegate{
internal var webView: WKWebView!
required init() {
webView = createWebView()
}
override func getView() -> UIView {
return webView
}
override func render() {
webView.navigationDelegate = self
// If I set a breakpoint before calling the loadHtmlString method, and wait a couple of seconds then the
// delegate is called succesfully
webView.loadHTMLString(html, baseURL: myBaseUrl)
}
}
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.evaluateBodyHeight()
}
}
我知道如果没有显示视图,则不会调用 WKWebView 的导航委托(我过去遇到过这个问题)。但在本例中,webview 被添加到一个正在显示和可见的视图容器中。
我想在设置断点时,操作系统有更多的时间来做它的事情,不知何故这会有所作为,但我不知道如何解决这个问题。
有什么想法吗?
提前致谢。