webViewDidStartLoad和webViewDidFinishLoad不起作用

时间:2018-06-15 13:15:10

标签: swift uiwebview

我正在尝试检查网页视图是否正在加载以及是否已完成加载,当我尝试搜索它时,这就是我所得到的但是它似乎不起作用是因为我已经完成了出了什么问题,或者他们改变了代码?我确实为网络视图设置了代理,所以我不认为问题出在代表处。

@IBOutlet weak var webView: UIWebView!
@IBOutlet weak var activity: UIActivityIndicatorView!

override func viewDidLoad() {
    super.viewDidLoad()
    let url = URL(string: "https://stackoverflow.com")

    let urlRequest = URLRequest(url: url!)
    webView.loadRequest(urlRequest)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func webViewDidStartLoad(_ webView: UIWebView){
    print("Webview started Loading")
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    print("Webview did finish load")
}

2 个答案:

答案 0 :(得分:1)

您需要使您的班级符合UIWebViewDelegate并将webView的管理委托给您的班级:

class YourClass: UIViewController, UIWebViewDelegate {
   @IBOutlet weak var webView: UIWebView!
   @IBOutlet weak var activity: UIActivityIndicatorView!

   override func viewDidLoad() {
     super.viewDidLoad()
     let url = URL(string: "https://stackoverflow.com")

     //!!!!!!!
     webView.delegate = self
     //!!!!!!!
     let urlRequest = URLRequest(url: url!)
     webView.loadRequest(urlRequest)
   }

   override func didReceiveMemoryWarning() {
       super.didReceiveMemoryWarning()
       // Dispose of any resources that can be recreated.
   }

   func webViewDidStartLoad(_ webView: UIWebView){
     print("Webview started Loading")
   }

   func webViewDidFinishLoad(_ webView: UIWebView) {
     print("Webview did finish load")
   }
}

答案 1 :(得分:0)

您需要在UIWebView中添加“delegate”

webView.delegate = self