cfc263fd43d875fa0dad42a51a8bbbb3.jpg
RACSubject
繼承于RACSignal
// RACSubject:底層實現和RACSignal不一樣。
1.調用subscribeNext訂閱信號,只是把訂閱者保存起來,并且訂閱者的nextBlock已經賦值了。
2.調用sendNext發送信號,遍歷剛剛保存的所有訂閱者,一個一個調用訂閱者的nextBlock
3.調用sendNext發送信號,不會保留原來的歷史值,把最新的數據傳遞給訂閱者
RACSubject *subect = [RACSubject subject];
[subect sendNext:@"數學"];
[subect subscribeNext:^(id x) {
NSLog(@"我訂閱了:%@",x);
}];
[subect sendNext:@"語文"];
[subect subscribeNext:^(id x) {
NSLog(@"你訂閱了:%@",x);
}];
[subect sendNext:@"歷史"];
2016-10-12 18:02:13.298 ReactiveCocoa基礎02[60479:413849] 我訂閱了:語文
2016-10-12 18:02:13.298 ReactiveCocoa基礎02[60479:413849] 我訂閱了:歷史
2016-10-12 18:02:13.299 ReactiveCocoa基礎02[60479:413849] 你訂閱了:歷史
通常用來代替代理
ViewController.m
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)click:(id)sender {
NextViewController *nextVC = [[NextViewController alloc] init];
// 設置代理信號
nextVC.subect = [RACSubject subject];
// 訂閱代理信號
[nextVC.subect subscribeNext:^(id x) {
//點擊了第二個界面
NSLog(@"%@",x);
}];
// 跳轉到第二個控制器
[self presentViewController:nextVC animated:YES completion:nil];
}
NextViewController.h
@interface NextViewController : UIViewController
@property(nonatomic ,strong)RACSubject *subect;
@end
NextViewController.m
@interface NextViewController ()
@property (nonatomic ,strong)UIButton *button;
@end
@implementation NextViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.button];
}
//懶加載按鈕
- (UIButton *)button{
if (!_button) {
_button = [UIButton buttonWithType:UIButtonTypeCustom];
_button.frame = CGRectMake(0, 0, 150, 50);
_button.backgroundColor = [UIColor whiteColor];
[_button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_button setTitle:@"點擊返回" forState:UIControlStateNormal];
[_button addTarget:self action:@selector(notice) forControlEvents:UIControlEventTouchUpInside];
_button.center = self.view.center;
}
return _button;
}
//點擊事件
-(void)notice{
[self dismissViewControllerAnimated:YES completion:nil];
if (self.subect) {
[self.subect sendNext:@"點擊了第二個界面"];
}
}
RACReplaySubject
繼承于RACSubject
區別:
RACReplaySubject可以先發送信號,在訂閱信號,RACSubject就不可以
同時會保留原來的歷史數據,等待訂閱者激活時接收數據
RACReplaySubject *replaySubject = [RACReplaySubject subject];
// 2.發送信號
[replaySubject sendNext:@1];
[replaySubject sendNext:@2];
// 3.訂閱信號
[replaySubject subscribeNext:^(id x) {
NSLog(@"第一個訂閱者接收到的數據%@",x);
}];
[replaySubject sendNext:@3];
// 訂閱信號
[replaySubject subscribeNext:^(id x) {
NSLog(@"第二個訂閱者接收到的數據%@",x);
}];
[replaySubject sendNext:@4];
2016-10-12 18:11:46.766 ReactiveCocoa基礎02[60909:424986] 第一個訂閱者接收到的數據1
2016-10-12 18:11:46.766 ReactiveCocoa基礎02[60909:424986] 第一個訂閱者接收到的數據2
2016-10-12 18:11:46.767 ReactiveCocoa基礎02[60909:424986] 第一個訂閱者接收到的數據3
2016-10-12 18:11:46.767 ReactiveCocoa基礎02[60909:424986] 第二個訂閱者接收到的數據1
2016-10-12 18:11:46.767 ReactiveCocoa基礎02[60909:424986] 第二個訂閱者接收到的數據2
2016-10-12 18:11:46.768 ReactiveCocoa基礎02[60909:424986] 第二個訂閱者接收到的數據3
2016-10-12 18:11:46.782 ReactiveCocoa基礎02[60909:424986] 第一個訂閱者接收到的數據4
2016-10-12 18:11:46.782 ReactiveCocoa基礎02[60909:424986] 第二個訂閱者接收到的數據4
by 有涯sui無涯