美文网首页
使用category为delegate和protocol解耦

使用category为delegate和protocol解耦

作者: 骑着老鼠虐大象 | 来源:发表于2017-11-22 15:37 被阅读154次

项目中我们有时候会遇到一些情况,需要经常使用某个代理,但是又不是都需要遵从协议,这时候就可以用我的这种方式完全解耦,一些小东西,欢迎指点和指指点点。下面直接开始:

要是大家闲看的麻烦,可以直接下载:https://github.com/IDwangluting/DeleagateOperation.git

demo 中 总共用了几个文件 ,
Viewcontroller,
newDeleagte.h,
UIViewController+Protocol,
UIViewController+Delegate

然后viewcontroller 中的代码是这样的:

import "ViewController.h"

import "UIViewController+Delegate.h"
import UIViewController+Protocol.h"

@implementation ViewController

-(void)viewDidLoad {
[super viewDidLoad];
self.delegate = self ;
[self addProtocol];
}

-(void)dealloc {
[self removeDelegate];
}

NewDeleagte.h 中的代码在下面,这里只有 协议的申明,

import <Foundation/Foundation.h>

@protocol NewDelegate <NSObject>

-(void)viewWillEnter;

-(void)viewDidEnter;

-(void)viewWillOut;

-(void)viewDidOut;

@end

UIViewController+Protocol.h 中的代码是这样的:

import "NewDelegate.h"

@interface UIViewController (Protocol)

@property (nonatomic,assign)id<NewDelegate> delegate ;

-(void)addProtocol ;

-(void)removeDelegate;

@end

UIViewController+Protocol.m 中的代码在下面,这里遵从了协议

import "UIViewController+Protocol.h"
import <objc/runtime.h>

@implementation UIViewController (Protocol)

-(void)setDelegate:(id<NewDelegate>)delegate {
objc_setAssociatedObject(self, @selector(delegate), delegate, OBJC_ASSOCIATION_ASSIGN);
}

-(id<NewDelegate>)delegate {
return objc_getAssociatedObject(self, @selector(delegate)); ;
}

-(void)addProtocol {

if (self.delegate  && [self.delegate respondsToSelector:@selector(viewWillEnter)]) {
  [self.delegate viewWillEnter];
}

if (self.delegate  && [self.delegate respondsToSelector:@selector(viewDidEnter)]) {
    [self.delegate viewDidEnter];
}

if (self.delegate  && [self.delegate respondsToSelector:@selector(viewWillOut)]) {
    [self.delegate viewWillOut];
}

if (self.delegate  && [self.delegate respondsToSelector:@selector(viewDidOut)]) {
    [self.delegate viewDidOut];
}

}

-(void)removeDelegate {
self.delegate = nil ;
}

UIViewController+Delegate.h 中的代码在下面,这里实现了代理

import "NewDelegate.h"

@interface UIViewController (Delegate) <NewDelegate>

@end

UIViewController+Delegate.m 中的代码是这样的:

import "UIViewController+Delegate.h"

@implementation UIViewController (Delegate)

-(void)viewWillEnter {
NSLog(@" line:%d method:%s",LINE,FUNCTION);
}

-(void)viewDidEnter {
NSLog(@" line:%d method:%s",LINE,FUNCTION);
}

-(void)viewWillOut {
NSLog(@" line:%d method:%s",LINE,FUNCTION);
}

-(void)viewDidOut {
NSLog(@" line:%d method:%s",LINE,FUNCTION);
}

其实这是前几天面试的时候,帮别人解决问题时想的一个方案,他当然大喜过望了。希望对大家有用吧!

相关文章

  • 使用category为delegate和protocol解耦

    项目中我们有时候会遇到一些情况,需要经常使用某个代理,但是又不是都需要遵从协议,这时候就可以用我的这种方式完全解耦...

  • Protocol,Delegate,Category,Exten

    @protocol是我们协议的标识,协议中的方法有两种,一种是必须实现的,另一种是可选择实现的。这是一种间接扩充功...

  • AFNetworking源码之AFAutoPurgingImag

    AFAutoPurgingImageCache图片缓存 通过2个protocol解耦,通过协议继承来解耦。协议相当...

  • delegate和block

    公共接口,方法较多也选择用delegate进行解耦 delegate的话,运行成本低,delegate只是保存了一...

  • 何时用Delegate和何时用Block?

    公共接口,方法较多也选择用delegate进行解耦 delegate的话,运行成本低,delegate只是保存了一...

  • MVC

    架构的中心就是 耦合解耦 :使不同的类或不同的模块之间交互影响越少,常用的解耦方式 Delegate Na...

  • Swift 中实现 Proxy,封装UITableView

    因为自己懒,不想每次都去弄tableVIew的delegate和dataSource,高大上的说法就是解耦。所以想...

  • iOS开发规范

    语言 使用US英语 代码组织 在函数分组和protocol/delegate实现中使用#pragma mark -...

  • delegate和protocol

    一直对protocol和delegate的认识十分模糊,今天看了些文章,由此记录一下。文章链接:http://ha...

  • 设计模式之业务代表模式

    简介 业务代表模式(Business Delegate Pattern)用于对表示层和业务层解耦。它基本上是用来减...

网友评论

      本文标题:使用category为delegate和protocol解耦

      本文链接:https://www.haomeiwen.com/subject/zvznvxtx.html