Premise of contact
The requirements for iOS development are gradually evolving with the development of The Times. I have seen a lot of JD requirements these days, and there is more or less a name – componentization.
Personal understanding of componentization
At the beginning, MY understanding of componentization was still very vague, and I always felt that it was high-end, grand and classy. It’s like you can’t even touch it. My understanding is to make several small functional components in the project private library, so that he does not depend on the project, reduce the coupling in the project. It’s actually used in general development. CocoaPods I understand is also a form of componentization.
CTMediator – broker
Personal understanding for the jump of the unified delivery center, a bit and logistics transfer station about the same. Unified handling of page jump logic.
How to use CTMediator
General jump to native page
#import "CTMediatorTestViewController.h" #import "CTMediatorPushViewController.h" @interface CTMediatorTestViewController () @property (nonatomic, strong) UIButton *normalPushButton; @end @implementation CTMediatorTestViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.normalPushButton]; [self.normalPushButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(100); make.left.equalTo(self.view).offset(10); }]; } # pragma mark - click event - (void) normalPushButtonTapAction: (UIButton *) button {CTMediatorPushViewController * normalPushVC = [[CTMediatorPushViewController alloc]init]; [self.navigationController pushViewController:normalPushVC animated:YES]; } #pragma mark - (UIButton *)normalPushButton{if (! _normalPushButton) { _normalPushButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_normalPushButton setTitle: @ "conventional Push" forState: UIControlStateNormal]; [_normalPushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_normalPushButton addTarget:self action:@selector(normalPushButtonTapAction:) forControlEvents:UIControlEventTouchUpInside]; } return _normalPushButton; } @endCopy the code
This is a very simple jump to a native page. #import “xxxxXXx. h” when we want to jump from one page to another ViewController. Over time the page will have many import files. If the ViewController can be used in other projects, why not include the #import “xxxxXXx. h” as well? This makes the project highly coupled.
CTMediator target-action redirects the page
We’ll still use the two controllers above.
1. Create a Target – the Action
We need to create a Target_Push class, and in the.h file we need to add a function to instantiate the target controller, as well as some assignment if we need parameters.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface Target_Push : NSObject
- (UIViewController *)Action_NativeToCTMediatorPushViewController:(NSDictionary *)params;
@end
Copy the code
Then we implement the function we defined in the.m file
#import "Target_Push.h"
#import "CTMediatorPushViewController.h"
@implementation Target_Push
- (UIViewController *)Action_NativeToCTMediatorPushViewController:(NSDictionary *)params{
CTMediatorPushViewController *pushVC = [[CTMediatorPushViewController alloc]init];
if ([params valueForKey:@""]) {
}
return pushVC;
}
@end
Copy the code
2. Create CTMediator Category
We define such a function in the created classification
#import <CTMediator/CTMediator.h>
#import "BaseViewController.h"
@interface CTMediator (PushActions)
- (BaseViewController *)dtp_mediator_ctmediatorPushViewControllerWithParams:(NSDictionary *)params;
@end
Copy the code
Here the ViewController uses BaseViewController and don’t forget if you use UIViewController \#import
. And then we implement this function
#import "CTMediator+PushActions. H "// 1. kCTMediatorTarget_Push the string is NSString * const in the XXX part of target_xxx. h kCTMediatorTarget_Push = @"Push"; / / 2. KCTMediatorActionNativeTo_NewsViewController is Target_xxx. H Action_xxxx function name defined in part of XXX nsstrings * const kCTMediatorActionNativeTo_NewsViewController = @"NativeToCTMediatorPushViewController"; @implementation CTMediator (PushActions) - (BaseViewController *)dtp_mediator_ctmediatorPushViewControllerWithParams:(NSDictionary *)params{ BaseViewController *viewController = [self performTarget:kCTMediatorTarget_Push action:kCTMediatorActionNativeTo_NewsViewController params:params shouldCacheTarget:NO]; If ([viewController isKindOfClass:[BaseViewController Class]]) {GGLog(@"%@ successfully instantiated page ", NSStringFromSelector(_cmd)); return viewController; }else{GGLog(@"%@ failed to instantiate the page ", NSStringFromSelector(_cmd)); return [[BaseViewController alloc]init]; } } @endCopy the code
Call the implementation
#import "CTMediatorTestViewController.h" #import "CTMediator+PushActions.h" @interface CTMediatorTestViewController () @property (nonatomic, strong) UIButton *normalPushButton; @end @implementation CTMediatorTestViewController - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.normalPushButton]; [self.normalPushButton mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view).offset(100); make.left.equalTo(self.view).offset(10); }]; } # pragma mark - click event - (void) normalPushButtonTapAction: (UIButton *) button {NSDictionary * params = @{@"title":@"CTMediatorPushViewController"}; BaseViewController *pushVC = [[CTMediator sharedInstance] dtp_mediator_ctmediatorPushViewControllerWithParams:params]; [self.navigationController pushViewController:pushVC animated:YES]; } #pragma mark - (UIButton *)normalPushButton{if (! _normalPushButton) { _normalPushButton = [UIButton buttonWithType:UIButtonTypeCustom]; [_normalPushButton setTitle: @ "Target - the Action to achieve page jump" forState: UIControlStateNormal]; [_normalPushButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_normalPushButton addTarget:self action:@selector(normalPushButtonTapAction:) forControlEvents:UIControlEventTouchUpInside]; } return _normalPushButton; } @endCopy the code
But the process doesn’t feel as simple as the previous direct Push. Maybe my current understanding is not deep enough ~~~