iOS RACSubject代替通知

RAC是一個很經常使用而且很好用的插件,簡潔的調用方式能夠代替不少原生方法,下面是RACSubject代替通知的使用方式:ide

#import <UIKit/UIKit.h>
#import <ReactiveObjC.h>

NS_ASSUME_NONNULL_BEGIN

@interface LWView : UIView
@property (nonatomic,strong)RACSubject * notifiSignal;
@end

NS_ASSUME_NONNULL_END


#import "LWView.h"

@implementation LWView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        self.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)setupUI{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = self.bounds;
    [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
}

- (void)click{ [self.notifiSignal sendNext:nil]; }
視圖內添加了一個按鈕,按鈕點擊時觸發點擊事件,咱們在viewController內使用視圖並接收通知
- (void)setupUI{
    LWView *view = [[LWView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, 300)];
    view.notifiSignal = [RACSubject subject];
    [view.notifiSignal subscribeNext:^(id  _Nullable x) {
        
//點擊按鈕須要處理的事便可寫在此處

    }];
    [self.view addSubview:view];
}
這樣就實現了RACSubject代替了通知
相關文章
相關標籤/搜索