Search code examples
swiftuistoryboardsegueback

Repeatedly use view controller for online app and still have a back button


I'm relatively new to programing in general and am currently designing a database project but now I ran into a logic problem which my head cant get straight.

The problem is that I have a view controller that loads in a list of things from the database and than when you click on it it opens a detail, from that detail there is a button that loads in a list like the previous one we came from so I use the same view controller and just request different data. So far, everything is nice and dandy.

But now comes the problem there is a back function so when I now press back once I go back to the detail and than when I press back a second time it loads the last data that I had loaded on that list so it loads the wrong data.

A good example of what I would like to have is the Facebook friend function when I click on my friends it loads a list and than I can click on their friends I see the same list with different data but when I go back two steps it loads in my friends again. I made it work with a very confusing and impractical system of registers but this is very bad because it sometimes throws errors.

So does anyone know how I could achieve what I want?


Solution

  • When you "reuse" this view controller, I would suggest creating a new instance of that view controller each time. This way, (assuming you avoid use of any global variables, but rather only use properties of the class) you're guaranteed that each time you create new instance of the view controller, it won't affect other instances that might exist elsewhere in the navigation controller's stack of previously instantiated view controllers.