【问题标题】:Prevent WKWebview from reloading when going back and forth from parent ViewController防止 WKWebview 从父 ViewController 来回时重新加载
【发布时间】:2021-07-18 09:11:30
【问题描述】:

我的应用中有两个ViewController,它们嵌入在NavigationController 中。

在第一个ViewController 中有一个按钮,它会推动到第二个ViewController。在第二个ViewController我有WKWebView,它会打开一个特定的URL。

每次用户在第二个ViewControllerreloads中来回访问网站。

我只想打开一次(第一次),并且当用户来回移动时会阻止它重新加载(因此用户不会丢失所有提供的数据,例如表单中的数据)。我怎样才能做到这一点?

我可以阻止第二个ViewController从导航堆栈中删除吗?

【问题讨论】:

    标签: ios swift uinavigationcontroller wkwebview


    【解决方案1】:

    我假设正在发生的事情是您正在创建一个新的ViewController 或重新加载它。解决此问题的一种方法是保存对第二个 ViewController 的引用并重复使用它。

    假设您对第二个ViewController 有类似的东西。

        var other: SecondViewController?
    

    你有一个前进到它的按钮。

        @IBAction func buttonPressed(_ sender: Any) {
            if let otherVC = other {
                navigationController?.pushViewController(otherVC, animated: true)
            } else {
                other = SecondViewController(nibName: "SecondViewController", bundle: nil)
                if let other = other {
                    navigationController?.pushViewController(other, animated: true)
                }
            }
        }
    

    现在,如果您来回移动,您将不会经历重新加载。

    【讨论】:

      猜你喜欢
      • 2015-11-04
      • 2014-08-28
      • 1970-01-01
      • 2010-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多