4

I have a view controller which has 5 different subviews, these subviews are changed according to some conditions, then there are functions which apply background color and add some minor aesthetics to the views.

I have segregated the functions in the viewcontroller only for views i.e Applying colors,adding views and button touch events etc.

Only for controllers ViewDidLoad, ViewDidAppear etc.

Rest I call it as model, as it does some calculations and other stuff. Calculating the constraints and constants for the view. Can anyone suggest me anyway how to incorporate the view, controller and model functions in separate class? I don't like everything crowding up in the same viewcontroller.

  • 3
    You should read this article. Among other things, it discusses the "massive view controller" specifically. – Robert Harvey Sep 7 '16 at 15:05
  • 1
    This topic can easily fill an entire book. In fact, at least one has been written. You should pick up a copy of "Working Effectively with Legacy Code". – RubberDuck Nov 6 '16 at 10:10
1

I suggest you add ViewModels.

For example say you have 'when Model.State = cancelled then colour is red'

Create a ViewModel with property Colour and a constructor which takes the Model as an argument and calculates and sets the colour.

Pass this ViewModel to the view rather than the model.

-3

Based on the the current state, one simple yet organized solution would be to create something like a "[YourViewController]Views" directory.

In this directory create separate class files for each subview and migrate their respective related code from your view controller file. You can make each one a subclass of UIView.

-3

Based on your issue you can also try to extend your view file to minor sub view modules and trigger them when the model changes.

protected by gnat Mar 21 '17 at 10:22

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.