框架:模块化Objection

框架:模块化

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值