When to subclass a UIViewController for a custom subview?

I saw custom subtasks implemented as a subclass of UIViewController , but perhaps this could be implemented as a subclass of UIView .

When should I subclass a UIViewController instead of a UIView for a preview? Are there any disadvantages for subclassing the UIViewController ?

+7
source share
4 answers

Personally, when I need logical logic, I do this with a subclass of UIViewController . Also, if I'm looking for some of the behavior that you get from a UIViewController , for example. presenting it in models or in the navigation controller.

If you are doing something fairly simple or easy, a UIView subclass is usually sufficient. I seem to have used them most often when creating custom buttons and table cells.

In my experience, I have found that I use additional subclasses of UIViewController than subclasses of UIView , but this may not be the best, it happens that I feel a little more comfortable using view dispatchers rather than direct views.

+6
source

See what Apple has to say Controller Objects and MVC Design Pattern

An iOS controller typically expects to complete at least one of the following roles:

  • Coordination controllers provide specific application logic. They respond to delegate messages, notifications and IBActions. Coordination controllers also establish connections between other objects and often control the creation and destruction of these objects.

  • View controllers, in particular, UIViewControllers, control the display of one screen content and trigger the transition to the next "screen". They respond to memory warnings and rotation events.

  • Mediation controllers exist in OS X, but their role is usually filled with view controllers in iOS. They act as a mediator between views and models; updating models when views receive input and updating views when models change.

If the behavior you implement fits into one of these categories, you probably want to create a controller object. If your logic concerns only the display of data (and, possibly, the response to user input), then perhaps it belongs to the presentation layer. If your logic is about the data itself, then it probably belongs to the model. If you cannot find the logic that suits your logic in any of these layers, you should probably model it differently as a combination of different responsibilities that belong to different objects in different layers. those. A view that requests data for display from a mediation view controller.

+4
source

You must also subclass UIViewController if you intend to use AdBannerView in your "view". AdBannerView requires a UIViewController to work.

+2
source

The next rule of thumb is: If you are doing a custom drawing, subclass UIView. Otherwise, a subclass of UIViewController.

+1
source

All Articles