Search code examples
iosuiviewcontrolleruiwebviewstatereusability

push viewController and keep the webView state ios


The first view of my app is HomeViewController which contain a webView. From an other view in the app I want to navigate (or push) my HomeViewController and in the same time I want to keep the first state of the webView.

How can I store and reuse the webView content?

Other possibility would be to store the viewController state and re-use it again. Any idea would be helpful?

Thanks.


Solution

  • I solved it by doing this.

    class HomeViewController: UIViewController, UIWebViewDelegate {
    
    class var instance: HomeViewController {
    
        struct Static {
            static var instance: HomeViewController?
            static var token: dispatch_once_t = 0
        }
        dispatch_once(&Static.token) {
            Static.instance = HomeViewController()
        }
        return Static.instance!
        }
    ..............................
    
    }
    

    And when I want to push again my HomeViewController I do this:

        self.navigationController?.pushViewController(HomeViewController.instance, animated: true)
    

    As simple as that.