Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single View Controller for multiple views

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 ?

like image 604
Raoul Avatar asked Jan 12 '17 20:01

Raoul


1 Answers

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)
}
like image 159
silicon_valley Avatar answered Sep 28 '22 19:09

silicon_valley