-1

I'm new to iOS and Swift and I have troubles figuring out how to navigate through views.

I'm trying to switch betweens views and being able to press back and next button as you wish without losing data (like textfields, spinner, etc.). I tried with "popToView" but as the name says, it will pop views which is not what I want.

What would be the best way to do such a thing?

The idea is being able to navigate through a form in multiple views. At the end, you have some kind of calculations with the values you filled. I want to be able to go back to previous views so I can change some fields. Once I did my modifications, I want to navigate to the final view again and see the new results.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Arromeidde
  • 45
  • 6
  • you want some kind of collection view or what? we don't know what you want to do, it is not normal way of navigating – Lu_ Mar 24 '17 at 10:10
  • present your second VC from first rather than pushing it, in that case both second VC and First VC will be in memory. Once you are done with changes in second VC and pop to First VC your FirstVC data are still maintained – Sandeep Bhandari Mar 24 '17 at 10:17
  • Passing data between views: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/27976793#27976793 How to use navigation view controller: https://www.google.com/search?q=navigation+controller+tutorial+swift&oq=navigation+controller+tutorial – App Dev Guy Mar 24 '17 at 10:18
  • 1
    You might want to use [`UIPageViewController`](https://developer.apple.com/reference/uikit/uipageviewcontroller), have a look at this tutorial [How To Create UIPageViewController Using Storyboard](http://www.appcoda.com/uipageviewcontroller-storyboard-tutorial/) – Adil Soomro Mar 24 '17 at 10:44

1 Answers1

0

This could be a design issue. You should normally have no data in the view themselves (e.g. content of text fields). The design pattern behind UIViewControllers assumes that you have a back end "model" with the data and that your views/view controllers are merely a means to convey that content to the user. So navigating between views should have no issues with content loss even if the views and controllers are released.

Besides, keeping a large number of views and controllers active in memory will eventually hit limits (iOS devices are not as memory rich as computers) and is generally not a good practice.

If you're supporting multiple devices, especially iPhone, popOvers and other presentation modes will automatically release views (as opposed to iPads that may keep them alive) so you need to be ready to handle both situations which means storing the data in your models, and not rely on views for that.

If you must keep them alive nonetheless, you simply have to retain a reference to the view controllers either in a global variable or array

Alain T.
  • 40,517
  • 4
  • 31
  • 51