gpt4 book ai didi

ios - swift 3 : Preload SecondViewController's WKWebView ahead of click

转载 作者:行者123 更新时间:2023-11-30 11:59:58 27 4
gpt4 key购买 nike

我需要一些帮助。我想在第一个 View Controller 中的 webView 完成后在第二个 View Controller 中预加载我的 webView。

我相信我需要使用通知中心发布/观察来调用第二个 View Controller 中的 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)
}


}

最佳答案

在第一个 VC 中:

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

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

在第二个 VC 中:

override func viewDidLoad() {
super.viewDidLoad()

// Add observer for your notification
NotificationCenter.default.addObserver(self, selector: #selector(loadWebView), name: NSNotification.Name(rawValue: "WebViewFinishedLoading"), object: nil)
}

//Remove observer in deinit
deinit {
NotificationCenter.default.removeObserver(self)
}

关于ios - swift 3 : Preload SecondViewController's WKWebView ahead of click,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47363962/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com