防止 WKWebview 从父级来回时重新加载 ViewController

Prevent WKWebview from reloading when going back and forth from parent ViewController

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

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

每次用户来回访问网站时 ViewController 重新加载。

我只想打开它一次(第一次),当用户来回打开它时会阻止它重新加载(这样用户就不会丢失所有提供的数据,f.e。在表单中).我怎样才能做到这一点?

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

我假设正在发生的事情是您正在创建一个新的 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)
            }
        }
    }

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