I have two ViewController
in my app, which are embedded in NavigationController
.
In the first ViewController
there is a button, which pushes to the second ViewController
. In the second ViewController
I have WKWebView, which opens a specific URL.
Every time the user goes back and forth the website in the second ViewController
reloads.
I want to open it only once (for the first time) and when the user goes back and forth prevents it from reloading (so the user does not lose all provided data, f.e. in the form). How can I achieve that?
Can I prevent the second ViewController
from deleting from the navigation stack?
I'm assuming that what is happening is that you are either creating a new ViewController
or reloading it. One way you can combat this is to save a reference to the second ViewController
and reuse it.
Say you have something like this for the second ViewController
.
var other: SecondViewController?
And you have a button that advances to it.
@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)
}
}
}
Now, if you go back and forth, the you wouldn't experience the reload.