Search code examples
iosswiftuiviewcontrolleruinavigationcontrolleruipageviewcontroller

Save all subsequent view controller states in UINavigationViewController


I have a container UIViewController that hosts a single UINavigationController. The container view controller has a button that opens a new view controller by calling present(newViewController, animated: true, completion: nil).

The newViewController has its own UINavigationController and also contains a button. That button can present another view controller that itself has a UINavigationController and another button and so on.

I want to keep that pattern going for as many iterations as possible and save the states of all of them. Is that possible? To close the current view controller I call _ = navigationController?.popViewController(animated: true) but that erases all the data from the previous view controller as well.


Solution

  • You asked two questions

    1) want to keep that pattern going for as many iterations as possible and save the states of all of them.

    Ans. : you can not continuously presenting view on already presented view controller. for that first you need to dismiss previous presented view.

    2) To close the current view controller I call

     _ = navigationController?.popViewController(animated: true) 
    

    but that erases all the data from the previous view controller as well.

    Ans. If you present any view controller then don't use popviewcontroller but use dismisViewController.