-1

I'm having a problem with the app I am developing right now : one of my UIViewControllers is huge ( around 3000 lines ).

For now, I created extensions of this view controller to handle delegate methods in order to "split" this controller. For instance, I have my main view controller called XYZMainViewController and as this controller is the delegate of a UITextField, I created an extension XYZMainViewControllerTextFieldDelegateExtension.swift in which I manage UITextFieldDelegate methods.

It's still pretty dirty to do that this way. I'm wondering what would be a good practice to handle huge controllers like this one.

Randy
  • 4,335
  • 3
  • 30
  • 64

2 Answers2

0

did you consider to design your app using MVVM (model view view model) the idea of MVVM is to avoid huge controllers. There are a lot of user guides out there which explain how to use MVVM in your IOS app. I was facing the same situation half a year ago and then i adopt MVVM in my app and i am very happy with it since my controllers are not huge, i can reuse code easily in my app and also it's much more easy to create unit tests. Please follow this guide which explain what is MVVM and how to use

ithttps://www.raywenderlich.com/74106/mvvm-tutorial-with-reactivecocoa-part-1

https://www.raywenderlich.com/74131/mvvm-tutorial-with-reactivecocoa-part-2

Ran Hassid
  • 2,788
  • 1
  • 12
  • 20
0

3000 lines is hmmmm hugeeeeee.

You have performed huge numbers of tasks in your controller. Controllers are supposed to control views.In my opinion(the way i create my iOS app structure) controller is suppose to perform view control,passing data from models to view(more specifically from classes which manipulates data).

"Separate of concerns" is important here. Don't implement your business logic inside controllers. There should be a separate module which do all of your business logic. How to manipulate models and complete a functionality. Then this class is used by controller to perform view updates.

No matter what type os view you have designed ,its controller can't be of 3000 lines. Delegation is not a problem. Controller is a good place to do delegation. Inside those delegate methods ,to perform some business is not the work of controller. There are some design patterns for IOS apps like MVC, MVVM.

But still you have to use "these design patterns efficiently. move the common functionality in one place etc. Implementation of business logic. Manipulation of models. Communication with DataBase.

Thanks.