框架:模块化
http://blog.jobbole.com/66929/
实现原理:先定义一个协议(protocol),然后通过objection来注册这个协议对应的class,需要的时候,可以获取该协议对应的object。对于使用方无需关心到底使用的是哪个Class,反正该有的方法、属性都有了(在协议中指定)。这样就去除了对某个特定Class的依赖。也就是通常所说的「面向接口编程」,因为我们无法直接获得对应的Class,所以必须要在协议里定义好对外暴露的方法和属性,然后该Class也要实现该协议。
注册模块、绑定接口对应的class
JSObjectionModule 1. 新建一个继承于JSObjectionModule的类,这个类像模块容器注册此模块提供的对象。 2. 注册需要重载两个方法,+load和-configure,load是向模块容器注册这个Module类,configure是向容器注册这个Module提供的功能。
//
// protocols.h
// objectionTest
//
#ifndef protocols_h
#define protocols_h
#import <Foundation/Foundation.h>
@protocol SecondViewProtocol <NSObject>
- (void)configureWithTag:(NSString *)tag;
@end
#endif /* protocols_h */
//
// SecondModule.h
// objectionTest
//
#import "JSObjectionModule.h"
NS_ASSUME_NONNULL_BEGIN
@interface SecondModule : JSObjectionModule
@end
NS_ASSUME_NONNULL_END
//
// SecondModule.m
// objectionTest
//
//
#import "SecondModule.h"
#import "SecondViewController.h"
#import "protocols.h"
#import <Objection.h>
@implementation SecondModule
+ (void)load
{
JSObjectionInjector *injector = [JSObjection defaultInjector];
injector = injector ? : [JSObjection createInjector];
injector = [injector withModule:[[self alloc] init]];
[JSObjection setDefaultInjector:injector];
}
- (void)configure
{
[self bindClass:[SecondViewController class] toProtocol:@protocol(SecondViewProtocol)];
}
@end
//
// ViewController.h
// objectionTest
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// objectionTest
//
#import "ViewController.h"
#import "protocols.h"
#import <Objection.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 50, 50);
[button setTitle:@"tiao" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClickeddd) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClickeddd {
UIViewController <SecondViewProtocol>* viewController = [[JSObjection defaultInjector] getObject:@protocol(SecondViewProtocol)];
[viewController configureWithTag:@"从第一个vc跳转过来"];
[self presentViewController:viewController animated:YES completion:nil];
}
@end
//
// SecondViewController.h
// objectionTest
//
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface SecondViewController : UIViewController
@end
NS_ASSUME_NONNULL_END
//
// SecondViewController.m
// objectionTest
//
#import "SecondViewController.h"
#import "ThirdVCViewController.h"
#import "protocols.h"
@interface SecondViewController ()<SecondViewProtocol>
@property (nonatomic,strong) UILabel * labell;
@property (nonatomic,copy) NSString * labellstring;
@end
@implementation SecondViewController
- (void)configureWithTag:(NSString *)tag {
self.labellstring = tag;
}
- (void)viewDidLoad {
self.title = @"SecondVc";
self.view.backgroundColor = [UIColor whiteColor];
[super viewDidLoad];
self.labell = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 100, 30)];
self.labell.backgroundColor = [UIColor grayColor];
self.labell.text = self.labellstring;
[self.view addSubview:self.labell];
UIButton * button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 200, 50, 50);
[button setTitle:@"到thirdVC" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClickeddd) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonClickeddd {
ThirdVCViewController * third = [[ThirdVCViewController alloc] init];
[self presentViewController:third animated:YES completion:nil];
}
@end