What is the best practice: Multiple UIViews or Multiple UIViewControllers

660 views Asked by At

I plan to develop an check list app that consists of multiple pages. Each page is going to display a list of simple Yes/No questions and a "Next" button at the bottom of the page.

In terms of the app design I see two options

  1. One UIViewController with a stack of full page UIViews (layered, z-index). Switching from page 1 to 2 I'll use sendSubviewToBack

  2. One UIViewController per page. Switching from page 1 to 2 I'll present the new ViewController.

Questions: What are are the PROS and CONS of these two approaches. Any other option that I've overseen? What are your experiences, suggestions?

3

There are 3 answers

0
Caleb On BEST ANSWER

Apple chose the one view controller per page approach in it's implementation of the same idea. That may be in part to maximize generality -- different users of UIPageViewController will obviously have different needs. But another consideration is memory; if you have a few dozen pages in the multiple subviews approach, you'll have a few dozen full-page subviews, and that's a quick way to use far more memory than you actually need at any given time.

If you do go with a single view controller and multiple subviews, consider instantiating the subviews only as you need them, and removing the ones that you don't need from the view hierarchy. Indeed, since it sounds like all your pages use the same format (checklist), it's hard to see why you'd ever need more than a single view for the pages. When the user selects a different page, you'd simply give the view a new set of data to draw.

0
Lord Zsolt On

Assuming you know how to write good code (reusing views / view controllers), they should have the same pros and same cons.

One pro in favour of the UIViewController approach is that it will most likely result in cleaner code. You also have the view lifecycle events like viewDidLoad, viewWillAppear, viewDidAppear, etc. which give you clearer view of what's happening.

0
Jonathan On

I think it depends of what you are going to do on your views. If both have the same behavior with the datas, I would use just one UIViewController. If you have to make more and different treatments, I would use two UIViewController.

Using two also make easier a beautiful transition (handle by iOS) between them. If you use one, maybe you should use a UIScrollView and add the two views inside and scroll programmatically.

And another way to do that is to use a UITableView to show directly all the datas in one view.