ReactiveCocoa中一些信号的转换及常用方法:
- 元组
- 包装
- 解析
//快速解析一个元组
RACTupleUnpack(NSNumber *number1,NSNumber *number2) = tupe;
NSLog(@"%@==>%@",number1,number2);
//快速包装一个元祖
RACTuple *tupe = RACTuplePack(@1,@2);
NSLog(@"%@",tupe);
- 解决强引用问题
@weakify(self);
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
@strongify(self);
self.age = 0;
return nil;
}];
//快速监听某个对象的某个属性改变
[RACObserve(self,age) subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
//为对象属性绑定信号源
RAC(self.btn,titleLabel.text) = self.tfd.rac_textSignal;
- 监听文本内容改变
//监听文本内容改变
[self.tfd.rac_textSignal subscribeNext:^(id x) {
NSLog(@"%@",x);
}];
-(void)moreSignal
{
RACSignal *signalA = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@1];
return nil;
}];
RACSignal *signalB = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
[subscriber sendNext:@2];
return nil;
}];
//有几个信号方法要有几个参数,会自动订阅信号
//必须同时存在
[self rac_liftSelector:@selector(updateUIWithHot:new:) withSignalsFromArray:@[signalA,signalB]];
}
-(void)updateUIWithHot:(NSString *)hot new:(NSString *)new
{
NSLog(@"hot ==>%@,new =>%@",hot,new);
}
- 监听通知相应事件
[[[NSNotificationCenter defaultCenter]rac_addObserverForName:UIKeyboardWillShowNotification object:nil] subscribeNext:^(id x) { }];
- KVO
[[self rac_valuesForKeyPath:@"age" observer:nil]subscribeNext:^(id x) {
NSLog(@"xxx==>%@",x);
}];
- event
[[_btn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
NSLog(@"btn-->UIControlEventTouchUpInside");
}];
- method
//将方法转换为信号(冷信号)
[[self rac_signalForSelector:@selector(viewWillAppear:)] subscribeNext:^(id x) {
NSLog(@"viewWillAppear");
}];