我试图获取初始视图(back_button)以在网页完全加载后将其自身删除,我做了很多研究,它们始终指向同一代码作为答案。但是,我的不会被呼叫。有人可以告诉我我在做什么错吗?
import UIKit
import WebKit
class ViewController: UIViewController {
@IBOutlet weak var LoadingView: UIImageView!
var webView: WKWebView?
var bgImage: UIImageView?
var imageViewObject :UIImageView?
override func viewDidLoad() {
super.viewDidLoad()
//var imageViewObject :UIImageView
let screenSize: CGRect = UIScreen.main.bounds
imageViewObject = UIImageView(frame:CGRect(x:0,y: 0, width: screenSize.width, height: screenSize.height))
imageViewObject?.image = UIImage(named:"back_image")
webView?.navigationDelegate = self as? WKNavigationDelegate
self.view.addSubview(imageViewObject!)
let myURL = URL(string: "https://www.google.com")
let myRequest = URLRequest(url: myURL!)
_ = webView?.load(myRequest)
}
override func loadView() {
super.loadView()
//self.view.sendSubview(toBack: imageViewObject)
//self.view = self.LoadingView
self.webView = WKWebView()
self.view = self.webView
}
@IBAction func BackButton(_ sender: Any) {
if(webView?.canGoBack)!
{
webView?.goBack();
}
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("TEST")
imageViewObject?.removeFromSuperview()
}
func webViewDidFinishLoad(webView: WKWebView) {
imageViewObject?.removeFromSuperview()
self.view = self.webView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
您必须通过将这一行代码放入viewDidLoad来符合它的协议委托
webView?.delegate = self
这样做之后,现在,如果要使用委托方法,则必须将viewController作为UIWebViewDelegate的子类
class ViewController: UIViewController ,UIWebViewDelegate{
答案 1 :(得分:-1)
我通过
修复了此问题webView?.naviationDelegate = self
并将其添加到类
class ViewController: UIViewController ,WKNavigationDelegate