Separating delegates from ViewController into custom classes

I'm new to iOS development, and I'm a little confused how to separate my code to avoid the huge ViewController.m file. Currently, my main view manager has quite a few delegates:

@interface ViewController : UIViewController <MKMapViewDelegate, HttpClientDelegate, CLLocationManagerDelegate, NSFetchedResultsControllerDelegate> 

I would like to reduce the viewController code, and I think I should create separate classes to handle delegate tasks. The problem is that if, for example, I create a singleton class for CLLocationManager and let it implement delegation methods, how do I communicate with the view controller?

Say my CLLocationManager gets a location update, how can I get viewController to make changes to the user interface? Should I use NSNotificationCenter to publish a notification and then add an observer to the view controller? Or is this the best way to just let viewController implement all the delegate methods as it is now?

+4
source share
2 answers

Move some of these functions to your data model.

It's hard to say how you should manage this, given the limited information you provided, but you need to wonder if one view controller really needs to control the map view and track the location and manage the HTTP connection and manage the selection of master data. Why not move some of them to your model, where it would be easier to split into modules?

Say my CLLocationManager receives a location update, how do I do I suggest the viewController make changes to the user interface? Should I use NSNotificationCenter will send a notification and then add an observer to the view controller?

Notification will be a good solution - it provides a way for the part of your program that manages the location (again, it probably belongs to the model) to report the change without meaning anything, in particular the parts of the program that takes care of the changes in location. It also allows one-to-many communication - if you have another view controller that also takes care of location, it can watch the same notification.

Or is the best way to simply allow viewController to implement all the delegate methods as it is now?

I try to think about sharing responsibilities accordingly more than limiting code size. If you have a class that does one job, but it takes a lot of code, this is great. If you have one class that manages many unrelated things, this is not so good. The problem is that many jobs seem to fall into the traditional "content management" role of the view controller. Try to separate the task of managing the presentation of the data (which is the legitimate task of the view controller) from the management of the data itself (which is the model task).

+1
source

Introduce the class responsible for delegation methods:

 @interface DelegateManager : NSObject <MKMapViewDelegate, HttpClientDelegate, CLLocationManagerDelegate, NSFetchedResultsControllerDelegate> -(id)initWithViewController:(ViewController*)vc; @property (weak) ViewController *delegate; @end 

In your ViewController:

 @interface ViewController : UIViewController -(void)doSomething; @end 

In your ViewController, create an instance of DelegateManager with the self as parameter. Set your delegate goals for your delegate. In the delegate methods of your DelegateManager, call [self.delegate doSomething]; to contact your ViewController.

0
source

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


All Articles