7

I'm trying to make a registration form with multiple views but only one view controller. After proceeding to the next view I'm writing the input to a struct which will be sent to the server later on. The problem I'm facing is that the VC is reinitialized when entering the new view and therefore the user struct is reinitialized too. Is there any way to get around having multiple ViewControllers ?

Raoul
  • 153
  • 1
  • 1
  • 11
  • How are you 'proceeding to the next view'? – silicon_valley Jan 12 '17 at 21:06
  • By triggering a segue – Raoul Jan 12 '17 at 21:10
  • Segues are always from one view controller to another. You'll have to use your single VC and do custom animated transitions like the ones I've described below. – Michael Dautermann Jan 12 '17 at 21:12
  • Are these different views for different steps in a single registration process? Or for different registration processes? A little description of how these views differ and their distinct functional intent might be helpful, too. – Rob Jan 12 '17 at 22:24
  • How about passing the struct via the segues to the next VC ? – Raoul Jan 13 '17 at 09:03
  • Yep, that is a good way to do it, but remember that `struct`s are value types, so you'd be passing copies around. You presumably need some mechanism to pass the final model back to update your model (e.g. some completion handler or delegate-protocol pattern). – Rob Jan 16 '17 at 02:49

4 Answers4

5

If the only reason for using one view controller is so that you can persist your data across the different screens you are trying to present, you should probably consider storing your data outside the view controller class. For example by using another class with a shared instance:

class DataContainer {

    static let shared = DataContainer()

    var someString: String?

}

You can now access the same data from any view controller as follows (without losing the data when moving to another view controller):

if let someString = DataContainer.shared.someString {
    print(someString)
}
silicon_valley
  • 2,639
  • 2
  • 16
  • 20
  • 1
    Note for readers, sharedInstance needs to be a type property, like 'static let shareInstance = DataContainer()' – Laser Mar 14 '18 at 23:12
0

It sounds like you are using a navigation controller to push new copies of the same View Controller as your user goes from view to view.

What I recommend doing is have your single view controller (which has a content view) and multiple subclassed UIViews.

When the user clicks "next" to go to the next view, animate moving the old view out of the way and bring the new one into position.

Checkout "Example II" of this tutorial. Instead of using image views, just use your own custom (subclassed) UIViews.

Here is a related question with more sample code.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

Create a Singleton Class Which should have a Static Object than your data won't be erased when you push or go to next views.

class RegistrationDataHelper {

    // MARK: - Variable Declarations
    static let shared = RegistrationDataHelper()
    
    var registrationData = [Your Data Type]

}

Now in your entire project anywhere you can access this data RegistrationDataHelper.shared.registrationData The Data won't be erased.

-1

What you need is to have a Parent View Controller that is holding the childViewController belonging to your registration path. Here is a simple tutorial in how to achieve this feature (in this case adding a Swipe Gesture Recognizer for navigation) made by Vea Software.

Tutorial

Like this all your childViewController can have their own custom class for logic, and you don't have to run a segue between them... just scroll to each other.

Pato Salazar
  • 1,447
  • 12
  • 21