04RAC-RACSubject

2018-05-18  本文已影响9人  myk
RACSubject和RACReplaySubject的区别:
RACSubject必须要先订阅信号之后才能发送信号, 而RACReplaySubject可以先发送信号后订阅.
步骤:
 // 1.创建信号
 RACSubject *subject = [RACSubject subject];
 
 // 2.订阅信号
 [subject subscribeNext:^(id x) {
 // block:当有数据发出的时候就会调用
 // block:处理数据
 NSLog(@"%@",x);
 }];
 
 // 3.发送信号
 [subject sendNext:value];
//第一个控制器中
FirstViewController *fist = [[FirstViewController alloc] init];
fist.subject = [RACSubject subject];

[fist.subject subscribeNext:^(NSString *x) {
     [self.btn setTitle:x forState:UIControlStateNormal];
}];
    
[[[[self.btn rac_signalForControlEvents:UIControlEventTouchUpInside]
       doNext:^(id x) {
         self.btn.enabled = NO;
 }] flattenMap:^RACStream *(id value) {
      return [self viewModel];
}] subscribeNext:^(NSNumber *isSuccess) {
       self.btn.enabled = YES;
       NSLog(@"isSuccess ==== %@", isSuccess);
       if ([isSuccess boolValue]) {
//跳转到第二个控制器
          [self.navigationController pushViewController:fist animated:YES];
       }
}];
//第二个控制器
.h文件
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic) RACSubject *subject;
@end

.m文件
-(void)btnAction:(UIButton *)sender{
    [self.subject sendNext:@"pop"];
    [self.navigationController popViewControllerAnimated:YES];
}
上一篇下一篇

猜你喜欢

热点阅读