CTMediator is mainly used for communication between componentized modules. In order to reduce the coupling degree between two modules after componentization, it is generally not recommended to use header files between them.

CTMediator is equivalent to the intermediary object of the middle layer. Through this intermediary object, module A calls module B, and A controller of A pushes to A controller of B. However, the controller in module A does not introduce the controller in module B, so the coupling degree between the two modules can be reduced.

Here is a small example of how to use CTMediator

  • Create a project
  • Podfile file pod install
platform :ios, '8.0'
target 'test' do
  pod 'CTMediator'
end
Copy the code
  • A successful import

  • Create a taxonomy of CTMediators

#import "CTMediator.h"
#import <UIKit/UIKit.h>

@interface CTMediator (CTMediatorModuleAActions)

- (UIViewController *)CTMediator_viewControllerForDetail;

@end

Copy the code
#import "CTMediator+CTMediatorModuleAActions.h"
NSString * const kCTMediatorTargetA = @"Test";
NSString * const kCTMediatorActionNativeFetchDetailViewController = @"nativeFetchDetailViewController";
@implementation CTMediator (CTMediatorModuleAActions)

// CTMediator encapsulation?

// CTMediator: pushLogin pushDetail

//

- (UIViewController *)CTMediator_viewControllerForDetail

{

    UIViewController *viewController = [self performTarget:kCTMediatorTargetA
        action:kCTMediatorActionNativeFetchDetailViewController
        params:@{@"key":@"value"}
        shouldCacheTarget:NO
    ];

    if ([viewController isKindOfClass:[UIViewController class]]) {

    // After the View Controller is delivered, it can be selected by the outside world as push or present

        return viewController;

    } else {

    // This is where exception scenarios are handled, depending on the product

        return [[UIViewControlleralloc] init]; }}Copy the code
  • My personal understanding this method is to encapsulate CTmediator method, which means that I want to call this method, you can find a call Target_Test an object to invoke a method called Action_nativeFetchDetailViewController, So here you need to create Target_Test class and then write Action_nativeFetchDetailViewController method implementation of this method.

  • target_test

This is where the jump really happens.

  • This opens TestViewController from this controller, which does not reference the test controller’s header file. Achieve communication between components while low coupling.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    UIViewController *viewController = [[CTMediator sharedInstance] CTMediator_viewControllerForDetail];

    // It is up to the user to decide whether to present or push the View Controller. The mediator simply gives an instance of the View Controller

    [self presentViewController:viewController animated:YES completion:nil];

}
Copy the code
  • The structure diagram is shown below