//KVO值监控,当alertTip改变时调用, filter对alertTip值进行过滤,此处当alertTip存在而长度不为0时,执行suscribeNext方法,弹出提示 [[RACObserve(self, alertTip) filter:^BOOL(NSString * value) { return value.length != 0 && value; }] subscribeNext:^(NSString * tip) { Alert(tip); }];
RAC(self.loginViewModel, userName) = self.userNameTF.rac_textSignal; // 作用: 将一个对象(self.loginViewModel) 的一个属性(userName) 和一个信号signal(rac_textSignal)绑定,当信号有变化的时候,绑定的值也对应变化 RAC(self.loginViewModel, password) = self.passwordTF.rac_textSignal; @weakify(self) //避免block造成的循环引用 ,给一个按钮(loginButton) 添加事件(点击事件), [[self.loginButton rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(id x) { @strongify(self) if (self.loginViewModel.userName.length == 0) { self.loginViewModel.alertTip = @"用户名不能为空"; return ; } if (self.loginViewModel.password.length == 0) { self.loginViewModel.alertTip = @"密码不能为空"; return; } [self.loginViewModel clickLogin]; }];
[[[self.textField1.rac_textSignal startWith:@"输入框长度必须大于3" ] filter:^BOOL(NSString * tf) { return tf.length > 3; }] subscribeNext:^(NSString * tf) { NSLog(@"%@",tf); }]; //
//startWith 说明作用
[[[self.textField1.rac_textSignal ignore: @"1"] startWith: @"忽略给定的值" ]subscribeNext:^(id x) { NSLog(@"%@",x); }];
//ignore 忽略给定的值
RAC(self.showLabel, text) = [RACObserve(self, name) distinctUntilChanged]; [RACObserve(self.showLabel, text) subscribeNext:^(id x) { NSLog(@"%@",x); }]; self.name = @"Bob"; self.name= @"Bob"; self.name= @"Bob2";
//distinctUntilChanged 当和上一次值相同时会忽略 (例如修改用户名的时候,向网络提交时,如果和上一次相同就不必要执行操作)
[[self.textField1.rac_textSignal takeUntilBlock:^BOOL(NSString * tf) { return [tf isEqualToString: @"stop"]; }] subscribeNext:^(id x) { NSLog(@"%@",x); }];
//当给定的signal完成前一直取值 (当输入框的值==stop前一直取值)
[[self.textField1.rac_textSignal take: 4] subscribeNext:^(id x) { QLog(@"%@",x); }];
//take 只会取前几次的值
_subject = [RACSubject subject]; [_subject subscribeNext:^(id x) { NSLog(@"%@",x); }]; [[_button2 rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(id x) { [_subject sendNext:@"点击按钮"]; }];
//RACSubject可充当信号也可以发送信号,通常用它来替代代理
RACCommand * command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) { NSLog(@"执行命令"); // return [RACSignal empty]; // 如果是空信号则返回 return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) { [subscriber sendNext: @"请求数据"]; [HttpRequestManager request_login: dic block:^(NSInteger code, NSString *msg, id content) { if (code == 200) { //请求成功 [subscriber sendNext: content]; [subscriber sendCompleted];//完成 } else { //请求失败 [subscriber sendError: nil]; } }]; return nil; }]; }]; self.command = command; //强引用命令,不要被销毁,否则接收不到数据 //订阅RACCommand中的信号 // [[_button2 rac_signalForControlEvents: UIControlEventTouchUpInside] executeCommand: command];
//绑定按钮事件 [[_button2 rac_signalForControlEvents: UIControlEventTouchUpInside] subscribeNext:^(id x) { [command execute: nil]; }]; [command.executionSignals subscribeNext:^(id x) { NSLog(@"%@",x); [x subscribeNext:^(id x) { NSLog(@"%@",x); }]; }]; //发出的最新信号,和上面代码效果相同(方式2)
[command.executionSignals.switchToLatest subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
// 和上面效果相同(方式3)
//flattenMap作用:把源信号的内容映射成一个新的信号,信号可以是任意类型
[command.executionSignals.switchToLatest flattenMap:^RACStream *(id value) {
return [RACStream return: command];
}];
[command.errors subscribeNext:^(NSError * error) { NSLog(@"%@",error); }];
//map 映射, skip 跳过前几次 [[[self.textField1.rac_textSignal map:^id(NSString * str) { return [NSString stringWithFormat:@"ABC-%@",str]; }] skip: 2] subscribeNext:^(NSString * str) { NSLog(@"%@",str); }];
//每隔1秒执行,类型定时器 [[RACSignal interval:1 onScheduler:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) { NSLog(@"%@",x); }];