ReactiveCocoa学习(二)

前端之家收集整理的这篇文章主要介绍了ReactiveCocoa学习(二)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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;
    }];
  • rac常用宏
//快速监听某个对象的某个属性改变
    [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");
    }];
原文链接:https://www.f2er.com/react/306620.html

猜你在找的React相关文章