Does it make sense to separate view controllers from views in iOS?

Based on the background of web development, which mainly uses MVC-based applications, I’m used to having components of my code separated in three groups of files: controllers, models, and views.

But in an iOS application, even using the MVC pattern, does it make sense to follow the same technique?

An UIViewController provides you with a default view in which you can add the rest of the subheadings ( UILabel , UIButton , ...) and have immediate access to them. And the language is the same, he doesn't like to deal with HTML / CSS and anything else.

But I came across some iOS applications where the UIView subclassed and maintained in a separate file, even if it will only be used in the UIViewController . Therefore, you must encode custom accessors to work with internal subzones.

I do not see the need for this, unless, of course, you reuse the same UIView in several places or do not execute your own drawing.

+4
source share
4 answers

Many of us, admittedly, will slightly grease the MVC lines and slip into our controllers with a bit of code related to browsing. I personally think that everything is in order. This does not diminish the fact that we still mainly follow the MVC pattern (and that Cocoa classes do a heavy lifting for presentation). But for me, if the code associated with the view goes beyond the trivial, I find that I choose a subclass of the view.

There will be people who are religiously subclass of UIView , but I think a little pragmatism is justified. If you add a trivial amount of code related to viewing, is your code clarity improved by abstracting from this in a new subclass of UIView ? Not always. But I think that this situation (when someone subclasses, when they probably do not need), is much less common than the opposite problem (when someone is not subclasses, when they probably should).

So you asked:

I do not see the need to do this [subclassing UIView], unless, of course, you reuse the same UIView in several places or do not execute your own drawing.

I agree that reuse and custom drawing (e.g. drawRect ) are two great examples. But I would also add the principle that if a subclassification would improve the clarity of the code, because the presentation is quite complex (admittedly a subjective call), then I will be a subclass. Two examples:

  • I had a calendar view in the application, and the view controller became cumbersome, managing all cells for different days of the month, etc. Thus, by subclassing UIView , I was able to abstract the ugly details of all subzones made up of a calendar view. I have subclassed not only the main “month”, but also the “daytime” views in the “month” view. No reuse as such. No custom drawing. But that was the right decision. The code is infinitely more legible.

  • I am increasingly subclassing UITableViewCell for my table views. In my first projects, I would have a table view controller cellForRowAtIndexPath to make all the complex composition and layout of this cell. My code is much easier to follow now that I usually subclass UITableViewCell in any situation where I don't use one of the standard cell types.

In short, although I agree that you should not force a subclass of a UIView for each view controller, I personally try to do this when the view reaches a certain level of complexity. I let code intelligibility manage my practices. I find that I subclass representations more than before, but by no means do this all the time.

On the bottom line, while you don’t need to constantly look at subclasses, I personally believe that many developers are guilty of the impossibility of subclassing (or models) when they should have. They should not do this because of some fanatical desire to comply with MVC, but rather should ask themselves if their code will be easier to read and maintain if they subclass the view or model as necessary.

+4
source

Normally I would have a UIViewController SingletonManager UIViewController actual data source. I prefer to keep things well separate. If I have a custom UIView where I will make my own drawing, I will have another file for this, because that makes sense. Even if I use only one place (you never know what you will need tomorrow, and you may need it elsewhere). Also, don't forget that you really have MVC well-divided in iOS:

  • NIBS / StoryBoard / Custom Drawing
  • UIViewControllers
  • Data source
+1
source

This might be a good template, since you are dealing with the difference between the same application on an iPhone vs. iOS. What could be a DetailViewController on an iPad, maybe a UINavigationController on an iPhone.

Another common pattern is that the controller is connected to a DataSource, while the view is agnostic for the DataSource and can be reused in multiple controllers.

0
source

Views are not in the view controller, it is either in separate classes or in the nib file loaded by the view controller, the view controller is written for this view, so it will have all the actions and outputs connected to this view.

Now, if you're asking about using a nib or subclass, it's the same as choosing between composition and inheritance.

If you need to have a special view, a subclass of UIView, but if you want to have a view with controls as subzones, use composition, and it's easier for you to use Interface Builder to compose your view.

The only reason I see the UIView subclass for subviews only is because of the best encapsulation, but since your controller is written specifically for presentation, there is no need for this encapsulation.

0
source

Source: https://habr.com/ru/post/1447499/


All Articles