1

I am coming from a C# wpf background where you can have one ViewModel handling multiple Views. This is a great way to share data amongst unrelated views. However I can't figure out how to do the same thing in iOS, as you seem to need a Controller for each View.

What I am trying to achieve is to have a sign up sequence where the user populates 5 screens of data one by one. I was going to use a PageViewController for this and each click on Next would transfer them to the next page in the sequence. All the while, adding all their input data to a parent model object which stayed around for all five screens, at the end you can submit the whole lot to the database to sign up.

The only way I can see so far to do this is to create five separate ViewControllers, one for each screen of the sign up, and create the navigation logic to display them as you click through. However this a) seems overkill and b) means each subsequent screen and viewcontroller doesn't know about the information the user entered in the previous steps.

What is the correct way to do this in iOS?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
NZJames
  • 4,963
  • 15
  • 50
  • 100

1 Answers1

0

You can do it in many ways. If you like to use UIPageViewController, you can actually have one view controller for all steps of the sign up process.

A main view controller would implement protocol UIPageViewControllerDataSource. It has two required methods, which return an instance of a content view controller. These instances can be of any UIViewController subclass, so you could have five separate view controller classes or you could have five instances of the same class.

Xcode has a project template "Page-Based Application". It might be a good sample code for you. There is one class DataViewController and it is instantiated for each page. If your case is really simple then this might be the best solution.

If you use multiple view controllers, you can pass data between them by overriding the method prepareForSegue(segue:sender:). The UIStoryboardSegue object has access to the destination view controller, you can cast it and set up the properties. (I am assuming you are using storyboards and segues.)

Another approach would be not to use UIPageViewController and implement the whole process within one view controller. There could be one view with five container subviews. Initially the first one would be shown, then the second one, and so on. This requires manual implementation of navigation between those steps, but gives a lot of flexibility. It's also more aligned with MVVM, because there is one-to-one mapping between a ViewModel and ViewController.

In general, iOS apps have lots of view controllers. It doesn't seem an overkill for me to use five in your case. It might be more code, but it's less complexity.

  • Brilliant, thanks very much. Just as an aside, two quick follow ups. If I use the PageViewControllerDataSource, is there a way to manually navigate and disable swiping? I want the navigation to be driven by button clicks only. And secondly is there a way to get those page index dots you see on multiple paged screens, which show which page of the collection you are on? I saw a PageController control in the Apple TV section but I want the same thing just for a basic iOS app. Thanks again! – NZJames Feb 12 '16 at 16:28