【问题标题】:I want to show web content simultaneoustly when switching from one screen to another我想在从一个屏幕切换到另一个屏幕时同时显示 Web 内容
【发布时间】:2020-09-24 12:49:00
【问题描述】:

我在网页视图上显示 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")   
    }
}

【问题讨论】:

    标签: ios swift wkwebview webviewdidfinishload


    【解决方案1】:

    将内容加载到 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)
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多