ReactiveCocoa常见类-RACSubject-信号提供者

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // RACSubject:信号提供者
    
    // 1.创建信号
    RACSubject *subject = [RACSubject subject];
    
    // 2.订阅信号
    [subject subscribeNext:^(id x) {
        // block:当有数据发出的时候就会调用
        // block:处理数据
        NSLog(@"%@",x);
    }];
    
    // 3.发送信号
    [subject sendNext:@1];
    
    // 开发中,使用这个RACSubject代替代理
}
//2016-08-14 22:22:06.352 ReactiveCocoa[37805:1736893] 1
- (IBAction)btnClick:(id)sender {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    TwoViewController *twoVc = [storyboard instantiateViewControllerWithIdentifier:@"two"];
    
    twoVc.subject = [RACSubject subject];
    
    [twoVc.subject subscribeNext:^(id x) {
       
        NSLog(@"通知了ViewController");
    }];
    
    [self presentViewController:twoVc animated:YES completion:nil];
    
}

//2016-08-14 22:25:21.425 ReactiveCocoa[38159:1740292] 通知了ViewController

你可能感兴趣的:(ReactiveCocoa常见类-RACSubject-信号提供者)