gpt4 book ai didi

ios - 在显示之前加载 ViewController WebView

转载 作者:行者123 更新时间:2023-11-28 07:40:54 26 4
gpt4 key购买 nike

我有一个带有包含 WebView 的 ViewController 的应用程序。因为 WebView 显示的网站非常大,所以我想在应用程序启动时加载 ViewController,这样一旦用户使用 WebView 打开 ViewController,它就会显示已经加载的页面。

我已经尝试覆盖 viewDidLoad()、loadView()、便利的 init() 等,但我放在那里的任何东西似乎都没有在实际显示 ViewController 之前执行。

 let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
webViewVC = storyboard.instantiateViewController(withIdentifier: "WebViewController")

if let c = webViewVC.childViewControllers[0] as? WebViewController {

c.dDelegate = self

webViewVC.modalPresentationStyle = .popover
let popover = webViewVC.popoverPresentationController!
popover.delegate = self
popover.permittedArrowDirections = .up
webViewVC.loadView()
present(webViewVC, animated: true, completion: nil) //if I don't add this line, the ViewController doesn't seem to get created before showing
}

那么我可以在 present() 之前调用什么来执行 ViewController 的一些代码?我需要加载如下内容:

let wwConfiguration = WKWebViewConfiguration()

webView = WKWebView(frame: CGRect(x: 0, y: 0, width: self.view.layer.frame.width, height: self.view.layer.frame.height), configuration: wwConfiguration)
webView.navigationDelegate = self
webView.uiDelegate = self
let url = URL(string: . . .

最佳答案

您的问题并不完全清楚,但我假设您正在从另一个 View Controller 中展示您的 Web View Controller ,比方说,按下按钮。像这样的……

enter image description here

你需要做的是异步处理这个。

  • 实例化您的 WebView Controller
  • 开始加载其 WebView
  • 等待完成
  • 展示 WebView Controller

例子……

WebView Controller

class WebViewController: UIViewController {

@IBOutlet private weak var webView: WKWebView!

var didLoadWebView: (() -> Void)!

func loadWebView(completion: @escaping () -> Void) {
loadViewIfNeeded()
didLoadWebView = completion // Save this closure to be called when loaded
webView.navigationDelegate = self
webView.load(URLRequest(url: URL(string: "http://apple.com")!))
}
}

extension WebViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
didLoadWebView() // Call back to the presenting view controller
}
}

呈现 ViewController

class ViewController: UIViewController {

@IBAction func showWebView(_ sender: UIButton) {

let wvc = storyboard!.instantiateViewController(withIdentifier: "WebViewController") as! WebViewController
wvc.modalPresentationStyle = .popover
wvc.popoverPresentationController?.sourceView = sender
wvc.popoverPresentationController?.sourceRect = sender.bounds
wvc.loadWebView {
self.present(wvc, animated: true, completion: nil)
}
}
}

关于ios - 在显示之前加载 ViewController WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52257950/

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