1.RACSubject:RACSubject:信号提供者,自己可以充当信号,又能发送信号。
2.使用场景:通常用来代替代理,有了它,就不必要定义代理了。
需求:
1.给当前控制器添加一个按钮,push到另一个控制器界面
2.另一个控制器view中有个按钮,点击按钮,返回控制器的第一个页面和接收到第二个界面的消息
第一个界面:
- -(void)viewDidLoad{
- [superviewDidLoad];
-
-
-
-
- //1.创建信号[RACSubjectsubject],跟RACSiganl不一样,创建信号时没有block。
- //2.订阅信号-(RACDisposable*)subscribeNext:(void(^)(idx))nextBlock
- //3.发送信号sendNext:(id)value
- //RACSubject:底层实现和RACSignal不一样。
- //1.调用subscribeNext订阅信号,只是把订阅者保存起来,并且订阅者的nextBlock已经赋值了。
- //2.调用sendNext发送信号,遍历刚刚保存的所有订阅者,一个一个调用订阅者的nextBlock。
- */
- self.view.backgroundColor=[UIColorwhiteColor];
- selfbuildUI];
- }
- -(void)buildUI{
- self.button.frame=CGRectMake(100,100,80,30);
- [self.viewaddSubview:self.button];
- }
- #pragmamark---lazyloading
- UIButton*)button{
- if(!_button){
- _button=[[UIButtonalloc]init];
- [_buttonsetBackgroundColor:[UIColorredColor]];
- [_buttonsetTitle:@"push"forState:UIControlStateNormal];
- [[_buttonrac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(idx){
- TwoViewController*twoVC=[[TwoViewControllerinit];
- twoVC.subject=[RACSubjectsubject];
- [twoVC.subjectidx){
- NSLog(@"%@",x);
- self.buttonsetTitle:x }];
- self.navigationControllerpushViewController:twoVCanimated:YES];
- return_button;
- }
TwoViewController
brownColor];
- -(void)buildUI{
- self.button.frame=CGRectMake(50,50,30);
- purpleColor];
- #pragmamark---lazyloading
- UIButton*)button{
- if(!_button){
- _button=[[UIButtongrayColor]];
- setTitle:@"pop"forState:UIControlStateNormal];
- [[_buttonidx){
- self.subjectsendNext:@"ws"];
- popViewControllerAnimated:YES];
- }];
- return_button;
- }
*总结:
我们完全可以用RACSubject代替代理/通知,确实方便许多
这里我们点击TwoViewController的pop的时候将字符串"ws"传给了ViewController的button的title。
步骤:
// 1.创建信号
RACSubject *subject = [RACSubject subject];
// 2.订阅信号
[subject subscribeNext:^(id x) {
// block:当有数据发出的时候就会调用
// block:处理数据
NSLog(@"%@",x);
}];
// 3.发送信号
[subject sendNext:value];
**注意:~~**
RACSubject和RACReplaySubject的区别
RACSubject必须要先订阅信号之后才能发送信号,而RACReplaySubject可以先发送信号后订阅.
RACSubject代码中体现为:先走TwoViewController的sendNext,后走ViewController的subscribeNext订阅
RACReplaySubject代码中体现为:先走ViewController的subscribeNext订阅,后走TwoViewController的sendNext
可按实际情况各取所需。 (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|