RACSignal
可以將RACSignal創(chuàng)建的對(duì)象傳遞至其他地方接收消息
//1.創(chuàng)建信號(hào)量
RACSignal * rac = [[RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//2.發(fā)出消息
[subscriber sendNext:@"RAC發(fā)出的消息"];
return nil;
//需要注意的是 如果有多個(gè)訂閱者 需要在這里加一個(gè)replay 否則不同訂閱者收到的不是同一條消息
}] replay];
//3.訂閱消息
[rac subscribeNext:^(id x) {
NSLog(@"打印收到的消息___%@",x);
}];
RACSubject
可以代替代理和block進(jìn)行反向傳值
TwoVC * vc = [[TwoVC alloc]init];
//創(chuàng)建信號(hào)量
vc.delegateSignal = [RACSubject subject];
//訂閱信號(hào)
[vc.delegateSignal subscribeNext:^(id x) {
NSLog(@"打印反向傳回的信號(hào)__%@",x);
}];
//RACSubject 可以有多個(gè)訂閱者接收消息
[vc.delegateSignal subscribeNext:^(id x) {
NSLog(@"打印第二個(gè)訂閱者收到的消息__%@",x);
}];
[self presentViewController:vc animated:YES completion:nil];
在TwoVC中發(fā)送消息
[_delegateSignal sendNext:@"RAC反向發(fā)出的信息"];
需要注意的是 RACSubject 只能先訂閱 再發(fā)送消息
RACReplaySubject
RACReplaySubject 可以解決 RACSubject 必須先訂閱后發(fā)送的問題
RACReplaySubject * replaySubject = [RACReplaySubject subject];
[replaySubject sendNext:@"RACReplaySubject先發(fā)送信息了楞艾,等待你的接收"];
[replaySubject subscribeNext:^(id x) {
NSLog(@"打印等待接收的信息__%@",x);
}];