ios - 我想在从一个屏幕切换到另一个屏幕时同时显示 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")
}
}
解决方案
将内容加载到 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)
}
}
推荐阅读
- javascript - 使用 DataTables rowGroup 时如何隐藏未分组的行值?
- google-mlkit - camera2 API Byte Buffer 输出上实时视频的姿势检测
- xml - XSLT1.0 复制所有节点并添加节点
- r - 如何在 R 中的大表中执行分析
- python - 了解模型 DEPTH 和 CUDA Out Of Memory 异常的依赖关系
- variables - 如何为 Wix Bundle Log Element 设置自定义值?
- html - 如何使用 CSS 变换将 div 放入 3D 图像
- javascript - 缩放时如何保持包装器居中?
- flutter - 在垂直PageView中包装不同高度的项目 - Flutter
- r - 修改 R 中 geom_link 函数的“大小”