没有调用Swift3 UIWebView委托方法

时间:2017-02-10 04:50:39

标签: swift uiwebview

代码:

let termWebView = UIWebView(frame:CGRect(x:0, y:20, width:320, height:400))
termWebView.delegate = self
self.view.addSubview(termWebView)

        if let url = URL(string: “http://www.google.co.in”)
            termWebView.loadRequest(request)
        }


        func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
        {

        }

       func webViewDidFinishLoad(webView: UIWebView!)
        {

        }

我创建了一个简单的webview并设置了委托。但是没有调用委托方法。代码有什么问题?任何帮助都会受到赞赏。谢谢提前

1 个答案:

答案 0 :(得分:0)

试试这个 -

class TouchUIViewController: UIViewController, UIWebViewDelegate {

 override func viewDidLoad() {
    super.viewDidLoad()

    let termWebView = UIWebView(frame:CGRect(x:0, y:20, width:320, height:400))
    termWebView.delegate = self
    self.view.addSubview(termWebView)
    let urlString = "http://www.google.co.in/"

    if let url = URL(string: urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)   {
        let request = URLRequest(url: url as URL)
        termWebView.loadRequest(request)
    }
  }

  func webViewDidStartLoad(_ webView: UIWebView) {

  }

  func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
  {

  }

  func webViewDidFinishLoad(_ webView: UIWebView)
  {

  }
}