How to achieve loose coupling
original article on why loose coupling is needed
http://martinfowler.com/articles/injection.html
how to achieve loose coupling in iOS
http://code.tutsplus.com/articles/design-patterns-delegation–cms-23901
If you do not do this…this will happen
https://blogs.msdn.microsoft.com/scottdensmore/2004/05/25/why-singletons-are-evil/
How we achieve loose coupling in Events App using protocols
In detail: UIViewController to Module
Make sure you conform to RecentActivityViewInterface so that you can receive data from delegate methods.
Then create a delegate that conforms to RecentActivityInterface so you can call methods that you need.
1 2 3 4 5 6 7 8 9 |
//conform to @interface HomePageViewController () <RecentActivityModuleDelegate> // your properties //create the interface delegate here so we can tell the other component what to do @property (nonatomic, strong) id<RecentActivityInterface> interface; @end |
ViewController.m
Implement the delegate method so we can receive the results:
1 2 3 4 5 |
#pragma mark RecentActivityViewInterface -(void)receivedData:(NSError*)error { NSLog(@"RECEIVED DATA!"); } |
ViewController.m
Hook everything up
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
-(void)initView { .... //get the singleton RecentActivityModule * recentActivityModule = [RecentActivityModule module]; //we need to implement the delegate methods to receive fresh data recentActivityModule.delegate = self; //then finally set our interface to the module to use it self.interface = recentActivityModule; ... } |
Then use it like so:
1 2 3 4 5 |
-(void)refresh { [self.refreshControl beginRefreshing]; [self.interface refreshData]; //call interface methods like this [self.refreshControl endRefreshing]; } |