0

For now I have a single login view controller that hide and adds subviews dynamically based on user interaction. Is it better to have multiple UIViews with one view controller, or multiple view controllers?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Depends on what you want to achieve, you could theoretically do everything is a single view controller, but your code would look poor, would be hard to maintain and would most probably not be optimized ( once again, depends on what you want to achieve .. ). Even if I don't know what your app si supposed to be, I am pretty sure you should create different view controllers. I would advise you to read tutorials about MVC. This one is really good : https://www.raywenderlich.com/46988/ios-design-patterns – Randy Jul 01 '16 at 17:21

3 Answers3

1

Of course its better to have multiple view controllers for many reasons:

  1. Each view controller has its own code/logic/functionality.
  2. Better for memory management. Once you're done from a view controller, system will automatically deallocates it from memory. Using multiple views will be overload in your system when some views are not used.
  3. Better organisation for your project. You will just present/dismiss/push/pop view controllers and pass data between each other. Instead you will deal with horrible logic for hiding and showing UIViews.
  4. Using one view controller will have a massive amount of code which in long term the project will be an impossible mission to manage.
Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
  • Thank you for the in depth answer. I am curious though, when you say "done from a view controller" in point two. How do you simply "clear" it from the memory?. Is it as simple as "view.removefromSuperView"? – Nash Equilibrium Jul 01 '16 at 17:29
  • @NashEquilibrium I certainly wouldn't want my entire application composed of UIViews I have to manually manage. However, if I have 5 "widgets" that I want to display on a single screen, I see no problem with using UIViews (i.e. UIView subclass) for those. I could either show and hide them, or add and remove them when they are not needed. Specifically for "clearing" from memory, it would just be, not have any reference to it, so in the case of the above description, view.removeFromSuperview as you say, but also make sure you don't keep reference to the child view controller either. – Matthew Horst Jul 01 '16 at 17:31
1

The benefit of having children UIViewControllers would be if you needed to care about the view lifecycle in any of the children. For instance, if in some of your subviews (child views) you needed to know if it appeared to trigger some logic, then it would be good to use children UIViewControllers.

Another reason might be, if you wanted to use view controller transitions within your custom container, as described by Apple in the UIViewController class reference: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/transitionFromViewController:toViewController:duration:options:animations:completion:

But, to me it doesn't sound like you would need this, it sounds like you would just show/hide your views, or otherwise animate them. But if your views don't care about any of that, and just need to render UI elements, I'd say using children UIViewControllers adds additional complexity without much benefit.

This is under the assumption that either way, all your views are living on the single UIViewController's view. If you are pushing and popping view controllers inside a UINavigationController, or presenting ViewControllers modally, you definitely should use UIViewControllers for that.

Also, you definitely shouldn't put all your view code inside that single view controller. You should use subclasses of UIView, or some other mechanism of making sure you separate your components.

For example you might have:

LoginViewController - organizes and hides and shows the individual views
SignInView - shows the sign in form
RegisterView - shows the register form
...etc

Matthew Horst
  • 1,991
  • 15
  • 18
0

Having multiple view controllers is better in the sense that you would have more ease at coding them individually. Also Navigation will be good which is good for User Experience. Memory Management will be efficient. The MVC architecture(Model View Controller) will instead become Massive View Controller which will be a headache while debugging.

Check out this answer for more clarity. I think you are confused between ViewController and View.

Check out the accepted answer of this question in the link. Hope it helps. About viewController's "viewDidLoad" and "viewWillAppear" methods

Community
  • 1
  • 1
Yash Tamakuwala
  • 1,789
  • 1
  • 24
  • 33