How evil is a UIViewController?

This is a somewhat open question about the UIViewController class and their proper role in an iOS application.

I recently read this article and I partially agree with the author. Obviously, it is important that every bit of logic associated with your views and models does not fall into the UIViewController , but it seems a bit extreme to build all of your views separately and use delegate methods to access actions.

I am wondering which design is most effective in terms of memory and application performance (which is clearly important when working with mobile applications)? As the author of the message noted, Apple does not seem to oppose introducing logic into the UIViewController .

Ultimately, I would like to know how to do things right . So the question is, should the associated views and any logic be completely separate from VC? Should I use delegates to communicate with the UIViewController ?

+7
ios objective-c uiviewcontroller
source share
2 answers

The view controller is not evil in nature, although it is common for them to turn into monolithic riots because they are so convenient for expansion.

... It seems a little extreme to create all of your views separately and use delegate methods to access actions.

Think about how to break your program into smaller units, whatever that is. A subclass of UIView in each case is not the best solution (as one example).

Each developer tolerance is slightly different and depends on the program / case, but it is quite easy to recognize and eliminate duplicate code and break your programs into smaller units.

I think most classes:

  • must not exceed several Ivars (e.g. 2)
  • no more than 100 lines required
  • must maintain composition over inheritance. in many cases, when you think you need inheritance, protocols can be used.

Of course there will be exceptions.

I am wondering which design is most effective in terms of memory and application performance (which is clearly important when working with mobile applications)?

It is important that you get a lot by writing more reusable programs. Invest more time and effort in these reusable projects, reduce duplicate code, and focus on quality. Write performance and memory in your projects where this is a concern. As a rule, this will lead to a big victory compared to the terrible recently written, poorly tested monolithic VC.

Ultimately, I would like to know the right way to do something. So, the question is, should all kinds and any logic connected with it be completely separated from VK? Should I use delegates to communicate with the UIViewController?

Relief: No, you don’t have to go that far if you eliminate redundant code, focus on reuse where applicable, and make sure your units / classes support low complexity. Absolutely, eliminate these problems before they become monolithic classes, regardless of whether they are VC or another type.

+6
source share

I also read this article a year ago when I was looking for better iOS coding methods than what you see everywhere (dumping all kinds of crap controllers ...). I agree with his points, although, unfortunately, he does not provide any solution to the problem. After a year of working on a rather large iOS application, with a complex data model, remote services and highly non-linear navigation, my impressions come down to the following points:

  • Use view controllers for a single user interaction element, for example, request login credentials, etc. A basic check (for example, checking empty fields, number formats, etc.) takes place in the view controller, but if the check is part of your company logic, it should be placed in your model.
  • Usually I have a so-called controller object that organizes the user interface stream and connects it to the domain model. The controller receives user input from view controllers through delegation mechanisms, so they are loosely coupled.
  • A single monolithic controller should be avoided; therefore, I usually try to split the functionality of a controller between simpler modular parts. For example, I have a controller that controls the registration process with a mostly linear user interface stream and its built-in to the main application controller, so its
+2
source share

All Articles