响应链信号传递

综合参考链接的两篇文章搞的

Update

2017-1-17

增强了一点功能“如果信号传到控制器,则向navigationController push出这个控制器之前的控制器传递”

用途

用在多层级的视图逆向传值,用来代替block和通知和代理

条件

需要是responder的子类,并且是在一条响应链上面,例如controller->view->view->button。

使用方法

//发送,例如
- (void)buttonClickAction:(UIButton *)sender {
    [sender sendYRUISignalForKey:YFTransferNameEvent userInfo:@{@"YFUserName" : [self userName]} callback:^(id receiver, id callbackData) {
        NSLog(@"%@",[receiver class]);
    }];
}
//实现方法,对信号进行拦截:
- (BOOL)handleYRUISignal:(YRUISignal *)signal {
    if ([signal.name isEqualToString:YFTransferNameEvent]) {
        self.resultLabel.text = signal.userInfo[@"YFUserName"];
        if (signal.callBack) {
            signal.callBack(self,nil);
        }
        return NO;
    } else {
        return YES;
    }
}

为responder建的一个分类

.h文件

#import 
typedef void (^YRUISignalCallBack)(id receiver, id callbackData);

@interface YRUISignal : NSObject
@property (retain, nonatomic) NSString *name; //名字,唯一标识
@property (assign, nonatomic) NSInteger tag;  //数字,辅助标识
@property (retain, nonatomic) id userInfo;    //附带信息或参数
@property (weak, nonatomic) id sender;   //发送者,应当是UIView或者UIViewController
@property (copy, nonatomic) YRUISignalCallBack callBack; //回调函数,可用于处理者调用
@end

@protocol YRUISignalDelegate 

/*!
 * @brief 处理某个信号,具体的是否处理该消息根据signal的name判别
 *
 * @param  signal  待处理的信号消息
 *
 * @return 如果返回是YES,则继续传递消息,如果是NO,则中断响应链
 */
@optional
- (BOOL)handleYRUISignal:(YRUISignal *)signal;

@end

@interface UIResponder (Router)

- (void)sendYRUISignalForKey:(NSString *)signalKey userInfo:(id)userInfo callback:(YRUISignalCallBack)callback;

@end

.m文件

#import "UIResponder+Router.h"

@implementation YRUISignal

@end

@implementation UIResponder (Router)

- (void)sendYRUISignalForKey:(NSString *)signalKey userInfo:(id)userInfo callback:(YRUISignalCallBack)callback {
    YRUISignal *signal = [[YRUISignal alloc] init];
    signal.name = signalKey;
    signal.userInfo = userInfo;
    signal.callBack = callback;
    [self sendYRUISignal:signal];
}
- (void)sendYRUISignal:(YRUISignal *)signal {
    id nextSender;
    if ([self isKindOfClass:[UIViewController class]]) {
        //如果信号传到控制器,则向navigationController push出这个控制器之前的控制器传递
        UIViewController *vc = (UIViewController *)self;
        if (vc.navigationController.childViewControllers.count > 0) {
            NSInteger index = [vc.navigationController.childViewControllers indexOfObject:vc];
            if (index > 0) {
                nextSender = vc.parentViewController.childViewControllers[index - 1];
            }
        }
    } else {
        //信号在响应链中传递
        nextSender = self.nextResponder;
    }
    if (nextSender) {
        if ([nextSender respondsToSelector:@selector(handleYRUISignal:)]) {
            if ([nextSender handleYRUISignal:signal]) {
                [nextSender sendYRUISignal:signal];
            } else {
                return;
            }
        } else {
            [nextSender sendYRUISignal:signal];
        }
    }
}

参考链接

iOS中UI事件处理YRUISignal
路由响应链方法进行传值

你可能感兴趣的:(响应链信号传递)