Swift 3:在点击之前预加载SecondViewController的WKWebView

时间:2017-11-18 08:09:37

标签: ios swift xcode preload tabbarcontroller

我需要一些帮助。 我想在第一视图控制器中的webView完成后在第二视图控制器中预加载我的webView。

我相信我需要使用通知中心post / observe在我的第二个视图控制器中调用我的loadWebView函数,但不知道如何操作。任何建议都表示赞赏。

enter image description here

FirstViewController.swift

import UIKit;
import WebKit;

class FirstViewController: UIViewController, WKNavigationDelegate {


    let webView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, UIScreen.main.bounds.height))

    func webView(_ webView: WKWebView,
             didFinish navigation: WKNavigation!) {
        print("loaded!")
        webView.isHidden = false

        }

    override func viewDidLoad() {
        super.viewDidLoad()
        let url = URL(string: "https://www.google.com")
        webView.navigationDelegate = self
        webView.load(URLRequest(url: url!))
        webView.isHidden = true
        self.view.addSubview(webView)

    }

}

SecondViewController.swift

import UIKit
import WebKit

class SecondViewController: UIViewController, WKNavigationDelegate {

    let webView:WKWebView = WKWebView(frame: CGRectMake(0, 0, UIScreen.main.bounds.width, UIScreen.main.bounds.height))


    func webView(_ webView: WKWebView,
             didFinish navigation: WKNavigation!) {
        print("loaded!")
        webView.isHidden = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    func loadWebView() {
        let url = URL(string: "https://www.amazon.com")
        webView.navigationDelegate = self
        webView.load(URLRequest(url: url!))
        webView.isHidden = true
        self.view.addSubview(webView)
    }


}

1 个答案:

答案 0 :(得分:0)

在第一个VC中:

<div class="slideshow-container">

            <ul class="rslides">
                <li><img src="img/img1.jpg" alt=""></li>
                <li><img src="img/img2.jpg" alt=""></li>
                <li><img src="img/img3.jpg" alt=""></li>
            </ul>
            <script src="js/sshow.js"></script>
        </div>
            <div id="continut"></div>
            <div id="continut2"></div>
            <div id="continut3"></div>
            <footer id="footer">
                <ul>
                    <li id="nume">x</li>
                    <li class="copyright">Copyright Ⓒ 2017</li>
                </ul>
            </footer>     
            </div>

在第二个VC中:

func webView(_ webView: WKWebView,
             didFinish navigation: WKNavigation!) {

   //Post notification
   NotificationCenter.default.post(name: NSNotification.Name(rawValue: "WebViewFinishedLoading"), object: nil)
}