无法将类型 'ViewController' 的值分配给类型 'WKNavigationDelegate?'
Cannot assign value of type 'ViewController' to type 'WKNavigationDelegate?'
我是 webkit 新手。
我想将我的 WKWebView 委托给我的视图控制器,但它给我错误:
Cannot assign value of type 'ViewController' to type 'WKNavigationDelegate?'
我的代码:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webKitComponent: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webKitComponent.navigationDelegate = self
}
}
添加
extension ViewController: WKNavigationDelegate {
private func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
private func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
private func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}
}
添加到@Sh_Khan的回答;错误的原因是 class ViewController
不符合开箱即用的协议 WKNavigationDelegate
。
您需要通过添加协议和所需方法使其符合要求,如 Sh_Khan 所示。
我是 webkit 新手。
我想将我的 WKWebView 委托给我的视图控制器,但它给我错误:
Cannot assign value of type 'ViewController' to type 'WKNavigationDelegate?'
我的代码:
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var webKitComponent: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
webKitComponent.navigationDelegate = self
}
}
添加
extension ViewController: WKNavigationDelegate {
private func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
debugPrint("didCommit")
}
private func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("didFinish")
}
private func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
debugPrint("didFail")
}
}
添加到@Sh_Khan的回答;错误的原因是 class ViewController
不符合开箱即用的协议 WKNavigationDelegate
。
您需要通过添加协议和所需方法使其符合要求,如 Sh_Khan 所示。