RAC - 02 - 使用RACSubject 代替 代理

2018-08-14  本文已影响0人  文瑶906

当想让 view 的数据告诉给 ViewController 的时候,需要使用代理。有些麻烦,这个时候可以使用 RACSubject 来代替 代理

#import <ReactiveObjC/ReactiveObjC.h>

@interface WYView : UIView
@property (nonatomic, strong) RACSubject *subject;
@end

@implementation WYView
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_subject sendNext:@"WYView"];
}
@end


// 调用
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    WYView *aView = [WYView new];
    aView.backgroundColor = [UIColor blueColor];
    aView.frame = CGRectMake(100, 100, 200, 200);
    [self.view addSubview:aView];

    RACSubject *subject = [RACSubject subject];
    [subject subscribeNext:^(id  _Nullable x) {
        // 订阅 代码
    }];
    aView.subject = subject;
}
@end
上一篇 下一篇

猜你喜欢

热点阅读