iosswiftuikitstoryboard

Reopen UINavigationController Modally With Previous State


Using storyboards, I have a button that modally presents a UINavigationController (with a UITableViewController as the root) that allows the user select rows to push additional views on the stack. The user can navigate down a hierarchy until they find the item they want. Selecting an item or swiping down closes the modal view.

If the user presses the button again, I would like to display the view with the same state as it was when it was previously closed (ie, opened with the same set of views pushed on the stack). Is it possible to hold on to the view when it closes and reuse it, or is there another way to maintain the state?

I looked into state preservation, but I don't want the state preserved if the app is closed and reopened, and I'm not sure it will handle this case in any event (at least, my attempt at implementing it didn't work).


Solution

  • Check apple docs SetViewControllers

    You can access the viewControllers property on UINavigationController to get the view controllers on the stack and save them to a local property.