我正在设置一个应用,其中在一个类中创建WKWebView
对象,然后将其传递给另一个类进行处理,但是当我在第二个类中使用didFinish navigation
时,它永远不会被呼叫。
我已经添加了WKNavigationDelegate
协议,并设置了navigationDelegate = self
class one: UIViewController {
var webView: WKWebView = WKWebView();
override func viewDidLoad(){
var second = Second()
second.web = webView;
second.test()
}
}
class second: NSObject, WKNavigationDelegate {
var web: WKWebView? = nil;
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("Test")
}
func test(){
self.web!.load(URLRequest(url: URL(string: "https://google.com")!))
}
}
我从没看到“测试”消息。
答案 0 :(得分:0)
使它成为实例var来保留对象,以便委托被调用
var second = Second() /// << here
override func viewDidLoad(){
super.viewDidLoad()
......
}
func test(){
self.web!.navigationDelegate = self
self.web!.load(URLRequest(url: URL(string: "https://google.com")!))
}
答案 1 :(得分:0)
import UIKit
import WebKit
class WKWebViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webWK: WKWebView!
@IBOutlet weak var activityIndi: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
webWK.navigationDelegate = self
let appStoreURL = URL(string: "https://google.com")
webWK.load(URLRequest(url: appStoreURL!))
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
activityIndi.startAnimating()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
activityIndi.stopAnimating()
}
}