首页 > 解决方案 > 我想在从一个屏幕切换到另一个屏幕时同时显示 Web 内容

问题描述

我在 web 视图上显示 html 字符串。但是在 webview 上显示需要 2-3 秒。当我从一个屏幕切换到另一个屏幕时,必须同时显示 Web 视图内容。我怎样才能做到这一点 ?

视图控制器1

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    @IBAction func actionOpenWebView(_ sender: Any) {
        let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2
        self.navigationController?.pushViewController(vc, animated: true)
    }    
}

视图控制器2

import UIKit
import WebKit

class ViewController2: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    
    let str = "Coconut couscous with berries, poppy seeds and natural yogurt Coconut couscous with berries, poppy seeds and natural yogurt Coconut couscous with berries, poppy seeds and natural yogurt"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        webView.loadHTMLString(str, baseURL: nil)
        webView.uiDelegate = self
        webView.navigationDelegate = self
    }
    
    @IBAction func actionBack(_ sender: UIButton) {
        self.navigationController?.popViewController(animated: true)
    }
    
}


extension ViewController2: WKUIDelegate, WKNavigationDelegate {
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        
        print("Show Web View Content Successfully")   
    }
}

标签: iosswiftwkwebviewwebviewdidfinishload

解决方案


将内容加载到 webView,一旦完成,然后将其呈现给用户(参见下面的代码)。

在 UIKit 中,对象之间或视图之间的通信有很多种方式。无论如何,在对象之间进行通信的最简单方法是 NotificationCenter。

import UIKit
import WebKit

class ViewController: UIViewController {
    
    var view2Controller: UIViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: .some(.init("webViewDidFinishNavigation")), object: nil, queue: nil) { _ in
            self.webViewDidFinishNavigation()
        }
    }

    @IBAction func actionOpenWebView(_ sender: Any) {
        let view2Controller = storyboard!.instantiateViewController(withIdentifier: "View2Controller") as! View2Controller
        // load a controller view without presenting the controller, this will execute viewDidLoad method which loads data
        _ = view2Controller.view
        // store view2Controller for a moment when a webview will finish content loading
        self.view2Controller = view2Controller
    }
    
    func webViewDidFinishNavigation() {
        self.navigationController?.pushViewController(view2Controller!, animated: true)
    }
}

class View2Controller: UIViewController, WKNavigationDelegate {
    
    @IBOutlet var webView: WKWebView!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        self.webView.navigationDelegate = self
        DispatchQueue.global().async {
            sleep(5)
            DispatchQueue.main.async {
                self.webView.loadHTMLString("dezdz d ezdze dze", baseURL: nil)
            }
        }

    }
    
    @IBAction func actionBack() {
        self.navigationController?.popViewController(animated: true)
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        NotificationCenter.default.post(name: .init("webViewDidFinishNavigation"), object: nil)
    }
}

推荐阅读