简体   繁体   中英

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 ?

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)
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM