Single View Controller for multiple views

こ雲淡風輕ζ 提交于 2020-08-24 06:48:26

问题


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 ?


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/41622845/single-view-controller-for-multiple-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!